无法反序列化 json 数据

Cannot deserialize json data

我无法反序列化我用于测试的以下数据,并使用以下查询从 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"
    }
  ]
]

我的主要 class 被称为 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; }
}

数组中使用的class称为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; }

}

国家和指标class定义如下:

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 正确调用 returns 数据,但每当我尝试使用 NewtonSoft JsonConvert.DeserializeObject 函数反序列化数据时:

PageModel pageModel = JsonConvert.DeserializeObject<PageModel>(data);

它returns 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"
 }
]
}