Deserializing Json array with variable names first using C# Json.NET

Viewed 3188

I'm getting an irregular JSON array from the Census Bureau's public api. The variable names are all in the first element, and I'm having trouble deserializing it.

http://api.census.gov/data/2014/pep/agesex?get=AGE,POP,SEX&for=us:*&DATE=7

gives me JSON like this:

[["AGE","POP","SEX","DATE","us"],
["0","3948350","0","7","1"],
["1","3962123","0","7","1"],
["2","3957772","0","7","1"],
["3","4005190","0","7","1"],
["4","4003448","0","7","1"],
["5","4004858","0","7","1"],
["6","4134352","0","7","1"],
["7","4154000","0","7","1"]]

I can successfully deserialize this using:

var test1 = JsonConvert.DeserializeObject<String[][]>(jsonStr);

However, I'm trying to deserialize it to a class like this:

public class TestClass
{
    public string AGE { get; set; }
    public string POP { get; set; }
    public string SEX { get; set; }
    public string DATE { get; set; }
    public string us { get; set; }
}

I'm trying to do this:

var test2 = JsonConvert.DeserializeObject<TestClass[]>(jsonStr);

But I'm getting the following exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot create and populate list type TestClass. Path '[0]', line 1, position 2.

5 Answers
Related