0

I have a set of data inside a Json file that represents the GPD evolution for each country every year. I'm trying to extract this data inside a list where every year would be a dictionary inside the list.

This is a part of the Json I'm trying to work with :

{
    "Name": "GPD per capita",
    "Description": "Real GDP per capita in 2011US$, 2011 benchmark (suitable for cross-country growth comparisons)",
    "Suffix": "$",
    "Multiplier": "1",
    "Data": [
        {
            "year": "1",
            "Belgium": "1050",
            "Switzerland": "1050",
            "Egypt": "1225",
            "Spain": "973",
            "France": "1050",
            "Greece": "1400",
            "Iran (Islamic Republic of)": "1225",
            "Iraq": "1225",
            "Israel": "1225",
            "Italy": "1546",
            "Jordan": "1225",
            "Portugal": "1050",
            "Tunisia": "1225",
            "Turkey": "984"
        },
        {
            "year": "730",
            "Egypt": "1278",
            "Iraq": "1610",
            "Japan": "633"
        },
        ...

And this is the class where the Json file should be deserialized :

[Serializable]
public class Collection
{
    public string Name;
    public string Description;
    public string Suffix;
    public ulong Multiplier;

    public List<IDictionary<string, ulong>> Data;
}

When I do Collection collection = JsonConvert.DeserializeObject<Collection>(jsonFile.ToString()); all the attributes show on the console but not the Data list.

How can I deserialize such structure correctly ?

Trafel
  • 79
  • 2
  • 10

1 Answers1

0

interfaces are not (De)Serializable since the Deserializer can not know as which final actual type it should Deserialize it.

Instead of an IDictionary simply rather use an actual Dictionary

public List<Dictionary<string, ulong> Data;

Alternatively you would need to add to the JSON the actual type in order to tell the Deserializer which type shall be used for the Deserialization.

See How to automatically select a concrete type when deserializing an interface using Json.NET

derHugo
  • 83,094
  • 9
  • 75
  • 115