【问题标题】:Deserialize a list of objects from Json Http response反序列化来自 Json Http 响应的对象列表
【发布时间】:2016-09-30 17:57:34
【问题描述】:

我的 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"
    }]
}

َ这是我的地址类:

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

我有这个 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);
    }
}

但是我收到了这个错误:

附加信息:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.IEnumerable`1[TestREST.Program+RestResponse]”,因为该类型需要 JSON数组(例如 [1,2,3])以正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3]) 或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,不是集合类型 像数组或列表)可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型中以强制它 从 JSON 对象反序列化。

路径“RestResponse”,第 2 行,位置 19。

我应该怎么做才能使我的反序列化工作?

【问题讨论】:

  • 请发布您的Address 课程。
  • 已更新地址类

标签: c# json serialization deserialization httpresponse


【解决方案1】:

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

类:

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

【讨论】:

  • 我得到的Json没有“\”这个。我有完全相同的字符串,但没有反斜杠,它不起作用。
  • @Pouyan ` \ ` 是为了转义 ` " ` 字符。因为我只是在 c# 代码本身中编写了字符串 (我的示例没有从服务器获取 json 数据) ...顺便说一句,效果很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
相关资源
最近更新 更多