无法反序列化JSON数据
问题描述:
我不能反序列化我使用测试下列哪些数据,并使用下面的查询得到了它从World Bank:无法反序列化JSON数据
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; }
}
cl在阵列中使用的屁股被称为数据和定义如下:
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; }
}
无论是国家和指标类定义如下:
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
。
任何想法为什么?
谢谢。
答
你的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"
}
]
}
+0
它适用于此JSON。试了一下自己! –
+0
@Satish,你是对的,但为什么世界银行会产生这样无效的JSON!没有道理!多么浪费时间,再次感谢您的帮助。 – Thierry
您是否尝试过一次反序列化部分字符串一类的调试?这样你可以更容易地找到问题。 – RhinoDevel
它看起来值,小数和日期字段的类型是字符串,而不是浮动,小数和日期。 –
@RhinoDevel我做到了。我删除了它的列表部分,看看我是否可以从PageModel获得基本信息,但是我不害怕!仍然返回null。 – Thierry