简体   繁体   English

反序列化来自Json Http响应的对象列表

[英]Deserialize a list of objects from Json Http response

I have my JSON Response like that: 我有这样的JSON响应:

{
  "Adddress": [
    {
      "Country": "United States",
      "City": "Irmo",
      "Line1": "103 Kinley Rd",
      "Line2": null,
      "PostalCode": "20063",
      "State": "SC",
      "AddressCode": "BILL-01"
    },
    {
      "Country": "United States",
      "City": "Irmo",
      "Line1": "1098 Kanley Road",
      "Line2": "Building B",
      "PostalCode": "29063",
      "State": "SC",
      "AddressCode": "SHIP-01"
    }]
}

َAnd Here is my Address Class: َ这是我的地址类别:

 [JsonObject()]
    public class Address
    {
        public string AddressCode { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Country { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public string City { get; set; }
    }

And I have this C# Code to deserialize this http response to my object list: 而且我有此C#代码可反序列化此http响应到我的对象列表:

HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
if (response.IsSuccessStatusCode)
{
    var dataObjects = response.Content.ReadAsAsync<Adddress>().Result;//JsonConvert.DeserializeObject<List<RestResponse>>(response.Content.ReadAsStringAsync().Result);//
    foreach (var d in dataObjects)
    {
        Console.WriteLine("{0}", d.Country);
    }
}

But I'm getting this error: 但我收到此错误:

Additional information: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[TestREST.Program+RestResponse]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 附加信息:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.IEnumerable`1 [TestREST.Program + RestResponse]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。

To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。

Path 'RestResponse', line 2, position 19. 路径“ RestResponse”,第2行,位置19。

What should I do with it to make my deserilization work ? 为了使反序列化工作有效,我该怎么办?

Adddress is single, the json you get in is an array of addresses (so more then one) , you have to deserialize it into eg AddressList that contains more then one address Adddress是单个的,您输入的json是一个地址数组(所以一个以上) ,您必须将其反序列化为例如包含多个地址的AddressList

string json = "{\"Adddress\":[{\"Country\":\"United States\",\"City\":\"Irmo\",\"Line1\":\"103 Kinley Rd\",\"Line2\":null,\"PostalCode\":\"20063\",\"State\":\"SC\",\"AddressCode\":\"BILL - 01\"},{\"Country\":\"United States\",\"City\":\"Irmo\",\"Line1\":\"1098 Kanley Road\",\"Line2\":\"Building B\",\"PostalCode\":\"29063\",\"State\":\"SC\",\"AddressCode\":\"SHIP - 01\"}]}";

 var dataObjects = JsonConvert.DeserializeObject<AddressList>(json);
 foreach (var d in dataObjects.Adddress)
 {
     Console.WriteLine("{0}", d.Country);
 }

Classes: 类别:

public class Adddress
{
    [JsonProperty("Country")]
    public string Country { get; set; }

    [JsonProperty("City")]
    public string City { get; set; }

    [JsonProperty("Line1")]
    public string Line1 { get; set; }

    [JsonProperty("Line2")]
    public string Line2 { get; set; }

    [JsonProperty("PostalCode")]
    public string PostalCode { get; set; }

    [JsonProperty("State")]
    public string State { get; set; }

    [JsonProperty("AddressCode")]
    public string AddressCode { get; set; }
}

public class AddressList
{
    [JsonProperty("Adddress")]
    public IList<Adddress> Adddress { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM