C# 从Json Http响应反序列化对象列表

C# 从Json Http响应反序列化对象列表,c#,json,serialization,deserialization,httpresponse,C#,Json,Serialization,Deserialization,Httpresponse,我的JSON响应如下: { "Adddress": [ { "Country": "United States", "City": "Irmo", "Line1": "103 Kinley Rd", "Line2": null, "PostalCode": "20063", "State": "SC", "AddressCode": "BILL-01" }, { "Coun

我的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);
    }
}
httpresponsemessageresponse=client.GetAsync(urlParameters).Result;//拦截呼叫!
if(响应。IsSuccessStatusCode)
{
var dataObjects=response.Content.ReadAsAsync().Result;//JsonConvert.DeserializeObject(response.Content.readastringasync().Result)//
foreach(数据对象中的变量d)
{
Console.WriteLine(“{0}”,d.Country);
}
}
但我得到了一个错误:

其他信息:无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.IEnumerable`1[TestREST.Program+Response]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化

要修复此错误,请将JSON更改为JSON数组(例如。 [1,2,3])或更改反序列化类型,使其成为普通的.NET 类型(例如,不是integer之类的基元类型,也不是集合类型 类似于数组或列表),可以从JSON对象反序列化。 还可以将JsonObjectAttribute添加到类型以强制其 从JSON对象反序列化

路径“重新响应”,第2行,位置19


我应该如何处理它才能使我的反序列化工作正常?

addAddress
是单个的,您得到的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);
 }
目前,该国的城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市:城市1号线:城市1号线:103近利道路:10 10.3万人;城市1号线:10.3万利利市:10.3万利利市:10.3万利利市:10.3万利利市,2号线;2号线:1号线:零,2号线:零,1号线:零,1号线;邮区;邮政政政局局局:1号:::::::,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“SC\,\”地址码\“:\“SHIP-01\”}]}”; var dataObjects=JsonConvert.DeserializeObject(json); foreach(dataObjects.addAddress中的变量d) { 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; }
}
公共类地址
{
[JsonProperty(“国家”)]
公共字符串国家{get;set;}
[JsonProperty(“城市”)]
公共字符串City{get;set;}
[JsonProperty(“第1行”)]
公共字符串Line1{get;set;}
[JsonProperty(“第2行”)]
公共字符串第2行{get;set;}
[JsonProperty(“PostalCode”)]
公共字符串PostalCode{get;set;}
[JsonProperty(“州”)]
公共字符串状态{get;set;}
[JsonProperty(“AddressCode”)]
公共字符串地址代码{get;set;}
}
公共类地址列表
{
[JsonProperty(“AddAddress”)]
公共IList地址{get;set;}
}

请发布你的
地址
类。用地址类更新我得到的Json没有这个“\”。我有完全相同的字符串,但没有反斜杠,它不起作用。@Pouyan的``是转义``字符。因为我只是在c代码本身中编写字符串(我的示例没有从服务器获取json数据)…顺便说一句,它能很好地工作