Json.NET反序列化根和子模式数据

时间:2015-09-29 20:38:18

标签: c# json json.net

我是JSON的新手,我正在从API调用中检索以下结构...

{
        "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"
                }        
            }
        ]
    }

我能够解析'客户'数据如下......

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;
}

但我很快就进入了地址&#39;数据。我尝试了一些我在其他帖子中看到的东西,但没有用。任何帮助将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

我使用了VS2015内置的JSON-&gt; Classes函数,它从你的JSON文件中生成了这个模式:

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; }
}

如果您愿意,Rootobject名称可以替换为CustomerList,或者您可以使用

将其反序列化为IEnumerable<Customer>
var jsonObj = JsonConvert.DeserializeObject<IEnumerable<Customer>>(json);

答案 1 :(得分:0)

您只需创建一个类来表示地址数据,并将Address属性添加到现有的Customer类中。

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; }
}

然后它应该可以正常工作:

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.
}

小提琴:https://dotnetfiddle.net/GiUdJs