Ryan
Ryan

Reputation: 185

How to deserialize a JSON array containing an object that contains an array of arrays with Json.NET and C#?

How can I deserialize the following JSON into a custom object using C# and Json.NET:

[ {
    'data': [
        [ '100473','3481282801.0' ],
        [ '100472','3481275600.0' ],
        [ '100471','3481273800.0' ],
        [ '100470','3481263900.0' ],
        [ '100469','3481263000.0' ]
    ],
    'error': '',
    'statement_time': 0.00440700000000000030,
    'total_time': 0.00475200000000000010
} ]

It is an array containing a single object. To make things more interesting the object contains an array of arrays. (I have no control over the format, unfortunately.) I am relatively new to JSON and thought I'd try Json.NET. I am open to other JSON frameworks if it would make things easier.

Upvotes: 0

Views: 678

Answers (2)

filipko
filipko

Reputation: 965

If you're using C# 4.0 or higher, use dynamic object:

dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);

Then you can access members of JSON using dot notation like this:

string s = json[0].data[0][0][1];

This, for example, will return you number 100473.

Upvotes: 0

I4V
I4V

Reputation: 35353

Using Json.Net

var result = JsonConvert.DeserializeObject<List<MyObject>>(str);

public class MyObject
{
    public List<List<string>> data { get; set; }
    public string error { get; set; }
    public double statement_time { get; set; }
    public double total_time { get; set; }
}

Upvotes: 4

Related Questions