c# – Json array of arrays to deserialized object list

I’ve seen other similar posts but none seem to cover when the json arrays do not have names.

Json string sample:

[
    [
        1618170480000,
        "59594.60000000",
        "59625.00000000",
        "59557.13000000",
        "59595.05000000",
        "32.64148000",
        1618170539999,
        "1945185.17004597",
        1209,
        "14.78751100",
        "881221.83660417",
        "0"
    ],
    [
        1618170540000,
        "59595.05000000",
        "59669.81000000",
        "59564.22000000",
        "59630.16000000",
        "27.45082600",
        1618170599999,
        "1636424.61486602",
        1066,
        "10.24907000",
        "610941.51532090",
        "0"
    ]
]

What I have currently working is:

List<List<object>> m = JsonConvert.DeserializeObject<List<List<object>>>(json_string);

and to print:

foreach(var l in m)
{
     foreach(var k in l)
     {
           Console.WriteLine(k);
     }
}

However, I want to deserialize to a class of properties (like I do with other return strings), say I have:

public class foo 
{
     public long prop1 { get; set; }
     public double prop2 { get; set }
     public (...)
}

So the first value in the array (1618170480000) is prop1 and the second is prop 2 etc.
I have this working when attributes in the Json string have names, but the return array is throwing me off.

I would expect it to look something like:

List<foo> m = JsonConvert.DeserializeObject<List<foo>>(json_string);

But this doesn’t work.

Any suggestions would be great.

Product of the Month September 2016

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *