-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"
     }
  }
}
Dilshod K
  • 2,924
  • 1
  • 13
  • 46

1 Answers1

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.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • thank you John. I'll try this one too. :) – Developer 2020 Mar 19 '20 at 04:41
  • @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. – ProgrammingLlama Mar 19 '20 at 04:43