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);

但我遗漏了一些东西,我无法正确获取信息。

4

2 回答 2

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"]);
    ...
}
于 2013-07-16T11:08:53.303 回答
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 检查它

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

于 2013-07-16T11:10:07.253 回答