【问题标题】:Cannot deserialize json data无法反序列化 json 数据
【发布时间】:2016-04-28 10:38:34
【问题描述】:

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

有什么想法吗?

谢谢。

【问题讨论】:

  • 您是否尝试过一次将部分字符串反序列化为一个类以进行调试?这样您可能更容易找到问题所在。
  • 看起来 value、decimal 和 date 字段是 string 类型,而不是 float、decimal 和 date。
  • @RhinoDevel 我做到了。我删除了它的列表部分,所以看看我是否可以从 PageModel 获得基本信息,但恐怕不好!仍然返回 null。
  • @TonPlooij 好的,我会试试的。
  • @TonPlooij - 将类型更改为字符串后不起作用。很奇怪!

标签: c# json deserialization json-deserialization


【解决方案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"
 }
]
}

【讨论】:

  • @Satish,你是对的,但世界银行到底为什么会生成如此无效的 JSON!没有意义!真是浪费时间,再次感谢您的帮助。
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多