我无法对我用于测试的以下数据进行反序列化,并使用以下查询从World Bank获取数据:
http://api.worldbank.org/countries/IRL/indicators/SP.DYN.CBRT.IN?
per_page=10&date=1960:2016&format=json
[
{
"page": 1,
"pages": 28,
"per_page": "2",
"total": 56
},
[
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.2",
"decimal": "0",
"date": "1961"
},
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.5",
"decimal": "0",
"date": "1960"
}
]
]
我的主要类叫做PageModel定义如下:
public class PageModel
{
public PageModel()
{
this.List = new List<Data>();
}
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("pages")]
public int Pages { get; set; }
[JsonProperty("per_page")]
public string PerPage { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
public List<Data> List { get; set; }
}
数组中使用的类称为Data,定义如下:
public class Data
{
public Data()
{
this.Indicator = new Indicator();
this.Country = new Country();
}
[JsonProperty("indicator")]
public Indicator Indicator { get; set; }
[JsonProperty("country")]
public Country Country { get; set; }
[JsonProperty("date")]
public int Date { get; set; }
[JsonProperty("value")]
public float Value { get; set; }
[JsonProperty("decimal")]
public decimal Decimal { get; set; }
}
Country和Indicator类的定义如下:
public class Country
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class Indicator
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
我的HttpClient
调用正确地返回数据,但每当我尝试使用NewtonSoft JsonConvert.DeserializeObject函数反序列化数据时:
PageModel pageModel = JsonConvert.DeserializeObject<PageModel>(data);
返回null
。
任何想法为什么?
感谢。
答案 0 :(得分:1)
您的JSON数据格式错误: 将您的json更改为此,它将起作用:
{
"page": 1,
"pages": 28,
"per_page": "2",
"total": 56,
"List":[
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.2",
"decimal": "0",
"date": "1961"
},
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.5",
"decimal": "0",
"date": "1960"
}
]
}