用数组反序列化Json

时间:2013-07-16 11:04:38

标签: c# json c#-4.0

我需要像这样反序列化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();
        js = new JavaScriptSerializer();
        var d = js.Deserialize<dynamic>(json);

但是我错过了一些东西,我无法正确获取信息。

2 个答案:

答案 0 :(得分:3)

数组数组数组的数组。纯粹的工程天才必须设计这种数据结构。

无论如何,这里是反序列化和访问其属性的方法:

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"]);
    ...
}

答案 1 :(得分:0)

你的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"
                    ]
                }
            ]
        ]
    ]
]

我用jsonlint.com检查它

我认为你的问题是因为你有嵌套数组。