uoah
uoah

Reputation: 169

Deserialize Json with array

I need to deserialize a json like this:

[[[[{"string1":"AB","date1":"01/01/1900 8:59:00","date2":"01/01/1900 9:28:00","col":["VO","SC","VD","LF","SR","TT","BN","MM","HH","HH","YY","ZZ"]}],[{"string1":"AB","date1":"01/01/1900 9:02:00","date2":"01/01/1900 9:30:00","col":["VO","SC","VD","LF","LP","VV","FF","MM","HH","HH","YY","ZZ"]}]]]]

I've tried with the following code:

JavaScriptSerializer serializer = new JavaScriptSerializer();
        js = new JavaScriptSerializer();
        var d = js.Deserialize<dynamic>(json);

But I'm missing something and I can't get the information correctly.

Upvotes: 0

Views: 172

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Array of arrays of arrays of arrays of arrays. A pure engineering genius must have designed this data structure.

Anyway, here's how to deserialize and access its properties:

string json = "[[[[{\"string1\":\"AB\",\"date1\":\"01/01/1900 8:59:00\",\"date2\":\"01/01/1900 9:28:00\",\"col\":[\"VO\",\"SC\",\"VD\",\"LF\",\"SR\",\"TT\",\"BN\",\"MM\",\"HH\",\"HH\",\"YY\",\"ZZ\"]}],[{\"string1\":\"AB\",\"date1\":\"01/01/1900 9:02:00\",\"date2\":\"01/01/1900 9:30:00\",\"col\":[\"VO\",\"SC\",\"VD\",\"LF\",\"LP\",\"VV\",\"FF\",\"MM\",\"HH\",\"HH\",\"YY\",\"ZZ\"]}]]]]";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var d = serializer.Deserialize<dynamic>(json);

foreach (dynamic item in d[0][0][0])
{
    Console.WriteLine(item["string1"]);
    Console.WriteLine(item["date1"]);
    Console.WriteLine(item["date2"]);
    ...
}

Upvotes: 3

user1968030
user1968030

Reputation:

Your json is valild

[
    [
        [
            [
                {
                    "string1": "AB",
                    "date1": "01/01/1900 8:59:00",
                    "date2": "01/01/1900 9:28:00",
                    "col": [
                        "VO",
                        "SC",
                        "VD",
                        "LF",
                        "SR",
                        "TT",
                        "BN",
                        "MM",
                        "HH",
                        "HH",
                        "YY",
                        "ZZ"
                    ]
                }
            ],
            [
                {
                    "string1": "AB",
                    "date1": "01/01/1900 9:02:00",
                    "date2": "01/01/1900 9:30:00",
                    "col": [
                        "VO",
                        "SC",
                        "VD",
                        "LF",
                        "LP",
                        "VV",
                        "FF",
                        "MM",
                        "HH",
                        "HH",
                        "YY",
                        "ZZ"
                    ]
                }
            ]
        ]
    ]
]

I check it with jsonlint.com

I think your problem because you have nested array.

Upvotes: 0

Related Questions