jrobinson6274 jrobinson6274 - 15 days ago 5
C# Question

deserializing JSON using NewtonSoft

I'm receiving the following error when I attempt to deserialize JSON text:

Unexpected character encountered while parsing value: {. Path '[0]', line 1, position 3.

The JSON Text validated properly using JSONLint.com, here is the JSON text:

[
[
{"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...

}

L.B L.B
Answer

The type you should use is 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; }
}
Comments