1

我正在尝试将 JSON 响应反序列化为不将 1 映射到 1 的对象。我尝试反序列化的 JSON 是

{
"CustomerID": "1",
"CustomerName": "John Doe",
"BillingAddressLine1": "1234 Main St",
"BillingAddressLine2": "APT. 5",
"BillingCity": "New York City",
"BillingState": "NY",
"BillingZip": "12345",
"BillingCountry": "USA",
"BillingAttention": "John Doe",
"MailingAddressLine1": "555 Main St",
"MailingAddressLine2": "P.O. Box 5",
"MailingCity": "New York City",
"MailingState": "NY",
"MailingZip": "12345",
"MailingCountry": "USA",
"MailingAttention": "Jane Doe"  
}

我试图反序列化它的对象是

public class Customer
{
    public int CustomerID{ get; set;}
    [JsonProperty(PropertyName = "CustomerName")]
    public string Name {get; set;}
    public Address BillingAddress{get; set;}
    public Address MailingAddress{get; set;}
}
public class Address
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
    public string Attention { get; set; }
}

有没有一种方法可以通过使用 Newtonsoft JSON 反序列化器进行映射,或者这是否需要自定义映射函数?

4

1 回答 1

0

奈尔,

  1. 首先,最好使用http://json2csharp.com/与 Json 一对一地生成对象。

  2. 将 Json 解析为这个对象。

  3. 调用第二种方法,将 Json-like-object 解析为您的对象。

简单,有点不好,但保持 KISS ......

于 2013-05-20T14:24:57.997 回答