反序列化 Json Http 响应中的对象列表
Deserialize a list of objects from Json Http response
我的 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"
}]
}
这是我的地址 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; }
}
我有这个 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);
}
}
但是我收到这个错误:
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[TestREST.Program+RestResponse]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g.
[1,2,3]) or change the deserialized type so that it is a normal .NET
type (e.g. not a primitive type like integer, not a collection type
like an array or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object.
Path 'RestResponse', line 2, position 19.
我应该如何处理它才能使我的反序列化工作正常进行?
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 回复是这样的:
{
"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"
}]
}
这是我的地址 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; }
}
我有这个 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);
}
}
但是我收到这个错误:
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[TestREST.Program+RestResponse]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'RestResponse', line 2, position 19.
我应该如何处理它才能使我的反序列化工作正常进行?
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; }
}