StackOverflow Q32853341

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
					
public class Program
{
	public static void Main()
	{
		string json = @"
            {
                ""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""
                        }
                    }
                ]
            }";
		
		var customerList = JsonConvert.DeserializeObject<CustomerList>(json);

		foreach (var obj in customerList.Customers)
		{
			Console.WriteLine("Name: " + obj.Name);
			Console.WriteLine("Code: " + obj.Code);
			Console.WriteLine("Contact: " + obj.Address.Contact);
			Console.WriteLine("Address1: " + obj.Address.Address1);
			Console.WriteLine("City: " + obj.Address.City);
			Console.WriteLine("State: " + obj.Address.State);
			Console.WriteLine("Postcode: " + obj.Address.Postcode);
			Console.WriteLine("Country: " + obj.Address.Country);
			Console.WriteLine();
		}
	}
}

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 string Code { get; set; }       
    public string Name { get; set; }
	public Address Address { get; set; }
}

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