使用NewtonSoft反序列化JSON

时间:2016-11-19 20:22:12

标签: c# json json.net

当我尝试反序列化JSON文本时,我收到以下错误:

解析值时遇到意外的字符:{。路径' [0]',第1行,第3位。

使用JSONLint.com正确验证了JSON文本,这里是JSON文本:

[
    [
        {"trackingNo":"R2E2003100011429","eventTime":1479184076000,"eventCode":"INF","activity":"Shipping Information received by Australia Post","location":"","referenceTrackingNo":null},
        {"trackingNo":"R2E2003100011429","eventTime":1479186149000,"eventCode":"INF","activity":"Shipping Information approved by Australia Post","location":"","referenceTrackingNo":null},
        {"trackingNo":"R2E2003100011429","eventTime":1479448620000,"eventCode":"SCN","activity":"Departed facility","location":"","referenceTrackingNo":null}
    ],
    [
        {"trackingNo":"HBF0003142011420","eventTime":1478666798000,"eventCode":"INF","activity":"Shipping Information received by Australia Post","location":"","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1478732453000,"eventCode":"INF","activity":"Shipping Information approved by Australia Post","location":"","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1478932980000,"eventCode":"SCN","activity":"Departed facility","location":"","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1479082255000,"eventCode":"SCN","activity":"Arrived at facility in destination country","location":"","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1479082261000,"eventCode":"CCD","activity":"Cleared by customs","location":"","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1479124118000,"eventCode":"SCN","activity":"Processed through Australia Post facility","location":"CHULLORA NSW","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1479236805000,"eventCode":"SCN","activity":"With Australia Post for delivery today","location":"KINGSGROVE NSW","referenceTrackingNo":null},
        {"trackingNo":"HBF0003142011420","eventTime":1479248135000,"eventCode":"DLD","activity":"Delivered","location":"CARINGBAH NSW","referenceTrackingNo":null}
    ]
]

Here is my code:

                    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                        {
                            string text = reader.ReadToEnd();
                                List<string[]> trackingResponse = JsonConvert.DeserializeObject<List<string[]>>(text);
//    ...Do some stuff...

        }

1 个答案:

答案 0 :(得分:2)

您应该使用的类型是List<List<SomeObject>>

var result = JsonConvert.DeserializeObject<List<List<SomeObject>>>(json);


public class SomeObject
{
    public string trackingNo { get; set; }
    public long eventTime { get; set; }
    public string eventCode { get; set; }
    public string activity { get; set; }
    public string location { get; set; }
    public string referenceTrackingNo { get; set; }
}