【问题标题】:Json.NET Deserializing Root and Subschema Data [duplicate]Json.NET反序列化根和子模式数据[重复]
【发布时间】:2015-09-30 06:41:47
【问题描述】:

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

但我在进入“地址”数据时遇到了麻烦。我尝试了一些我在其他帖子中看到的东西,但无济于事。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 这几样东西是什么?
  • 有趣的是,我住在威斯康星州的德佩尔。您是否尝试过使用 JSON->C# 生成器为代码生成类? VS2015 内置了一个。
  • rene - 抱歉,我不知道我是怎么错过那个帖子的。
  • Ron Beyer - 我希望我早点了解 JSON 2 C#。它会为我节省很多时间。谢谢!

标签: c# json json.net


【解决方案1】:

您只需要创建一个类来表示地址数据,并将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

【讨论】:

  • 谢谢布赖恩,成功了!我正在添加一个 List 类。哇!
  • 没问题;很乐意提供帮助。
【解决方案2】:

我使用了 VS2015 内置的 JSON->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&lt;Customer&gt;
var jsonObj = JsonConvert.DeserializeObject<IEnumerable<Customer>>(json);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多