0

I'm new to JSON and I'm retrieving the following structure from an API call...

{
        "Customers":[
            {
                "Code":"11111",
                "Alpha":"A",
                "Name":"Test A",            
                "Address":{
                    "Contact":"John Doe",
                    "Address1":"PO Box 111",
                    "Address2":"",
                    "Address3":"",
                    "City":"DE PERE",
                    "Postcode":"54115",
                    "State":"WI",
                    "Country":"USA"                
                }  
            },
            {
                "Code":"22222",
                "Alpha":"B",
                "Name":"Test B",
                "Address":{
                    "Contact":"Jane Doe",
                    "Address1":"PO Box 222",
                    "Address2":"",
                    "Address3":"",
                    "City":"DE PERE",
                    "Postcode":"54115",
                    "State":"WI",
                    "Country":"USA"
                }        
            }
        ]
    }

I'm able to parse the 'Customers' data with the following...

public class Customer
{
    public string Code { get; set; }       
    public string Name { get; set; }         
}

public class CustomerList
{
    public List<Customer> Customers { get; set; }        
}    

dynamic jObj = JsonConvert.DeserializeObject(json);
dynamic jsonObj = JsonConvert.DeserializeObject<CustomerList>(json);

foreach (var obj in jsonObj.Customers)
{
    string Name = obj.Name;
    string Code = obj.Code;
}

But I'm having a heck of a time getting into the 'Address' data. I tried a few things I saw in some other posts, to no avail. Any help would be appreciated.

Thanks

2 Answers2

0

I used VS2015 built-in JSON->Classes function and it generated this schema from your JSON files:

public class Rootobject
{
    public Customer[] Customers { get; set; }
}

public class Customer
{
    public string Code { get; set; }
    public string Alpha { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string Contact { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string City { get; set; }
    public string Postcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

The Rootobject name can be replaced with CustomerList if you want, or you can just deserialize to an IEnumerable<Customer> using

var jsonObj = JsonConvert.DeserializeObject<IEnumerable<Customer>>(json);
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
0

You just need to create a class to represent the address data, and add an Address property to your existing Customer class.

public class Address
{
    public string Contact { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string City { get; set; }
    public string Postcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    ...
    public Address Address { get; set; }
}

Then it should work just fine:

var customerList = JsonConvert.DeserializeObject<CustomerList>(json);

foreach (var obj in customerList.Customers)
{
    string Name = obj.Name;
    string Code = obj.Code;
    string Contact = obj.Address.Contact;
    string City = obj.Address.City;
    // etc.
}

Fiddle: https://dotnetfiddle.net/GiUdJs

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300