Search code examples
c#jsondeserializationjson-deserialization

Cannot deserialize json data


I cannot deserialized the following data which I'm using for testing and got it from the World Bank using the following query:

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

My main class is called PageModel is defined as such:

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; }
}

The class used in the array is called Data and is defined as follows:

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; }

}

Both the Country and Indicator classes are defined as follows:

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; }
}

My HttpClient call returns the data correctly but whenever I try to deserialize the data using the NewtonSoft JsonConvert.DeserializeObject function:

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

It returns null.

Any ideas why?

Thanks.


Solution

  • Your JSON data is in wrong format: Change your json to this and it will work:

    {
    "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"
     }
    ]
    }