-1

I just wanna ask if how to implement in c# a json data with array structure. This is the sample data below:

{
 "contact":
  {
    "contact_type_ids": ["CUSTOMER"],
     "name":"JSON Sample ResellerAVP",
     "main_address":
     {
        "address_type_id":"ACCOUNTS",
         "address_line_1":"Ayala Hills",
         "city":"Muntinlupa",
         "region":"NCR",
         "postal_code":"1770",
         "country_group_id":"ALL"
     }
  }
}
3
  • 2
    json2csharp.com generates c# classes based on the json input given . Use Serialization/Deserialization to fill the classes . Commented Mar 19, 2020 at 4:25
  • 2
    Visual Studio has this tool. Copy your string json, go to Edit->Paste Special->Paste Json as classes Commented Mar 19, 2020 at 4:26
  • Oh thanks guys. I'll try your suggestions. :) Commented Mar 19, 2020 at 4:29

1 Answer 1

0

JSON overview:

  • { } - Object
  • [ ] - Array
  • "a": something - Property

A property can have an object, array, or value type as its value. For example:

{
    "a": true,
    "b": "hello",
    "c": 5.2,
    "d": 1,
    "e": { "eChildProperty": "test" },
    "f": [ "a", "b", "c" ]
}

Let's start transcribing this JSON into classes!

{
 "contact":
  {
    "contact_type_ids": ["CUSTOMER"],
     "name":"JSON Sample ResellerAVP",
     "main_address":
     {
        "address_type_id":"ACCOUNTS",
         "address_line_1":"Ayala Hills",
         "city":"Muntinlupa",
         "region":"NCR",
         "postal_code":"1770",
         "country_group_id":"ALL"
     }
  }
}

OK, so we have a root object with a property "contact", which is also an object. Let's represent both of those:

public class RootObject
{
    public Contact Contact { get; set; }
}

public class Contact
{

}

Now we need to add Contact's properties. It has 3: contact_type_ids is an array of strings, name is a string, and main address is a complex object. Let's represent those:

public class Contact
{
    [JsonProperty("contact_type_ids")]
    public IList<string> ContactTypeIds { get; set; } // I'm using an IList, but any collection type or interface should work

    public string Name { get; set; }

    [JsonProperty("main_address")]
    public Address MainAddress { get; set; }
}

public class Address
{

}

Finally we need to work on the Address object:

public class Address
{
    [JsonProperty("address_type_id")]
    public string AddressTypeId { get; set; }

    [JsonProperty("address_line_1")]
    public string AddressLine1 { get; set; }

    public string City { get; set; }

    public string Region { get; set; }

    [JsonProperty("postal_code")]
    public string PostalCode { get; set; }

    [JsonProperty("country_group_id")]
    public string CountryGroupId { get; set; }
}

Putting this all together we get:

public class RootObject
{
    public Contact Contact { get; set; }
}

public class Contact
{
    [JsonProperty("contact_type_ids")]
    public IList<string> ContactTypeIds { get; set; } // I'm using an IList, but any collection type or interface should work

    public string Name { get; set; }

    [JsonProperty("main_address")]
    public Address MainAddress { get; set; }
}

public class Address
{
    [JsonProperty("address_type_id")]
    public string AddressTypeId { get; set; }

    [JsonProperty("address_line_1")]
    public string AddressLine1 { get; set; }

    public string City { get; set; }

    public string Region { get; set; }

    [JsonProperty("postal_code")]
    public string PostalCode { get; set; }

    [JsonProperty("country_group_id")]
    public string CountryGroupId { get; set; }
}

And we can use it like so:

RootObject deserialized = JsonConvert.DeserializeObject<RootObject>(jsonString);

Try it online


JSON isn't complicated by any stretch of the imagination. It's really simple to understand and manually convert into classes just by looking at the data you have.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you John. I'll try this one too. :)
@Developer Please try to understand how we get from the JSON to the C# classes or from the C# classes to the JSON. It will probably help you much more than simply finding out how to use tools to convert your JSON into classes, especially since those tools don't always get it right or don't choose the correct C# types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.