简体   繁体   中英

Deserialize nested Json into a list

I would like to deserialize the following nested json. To do this I need the data: - state, lat, lng, time - from the json into a list. On the Newtonsoft site I can't find any information that helps me. Does anyone have an idea how I can deserialize this json?

    [
    {
    "data":[
        {
        "state":"STOP",
        "lat":51.99225976,
        "lng":6.97070897,
        "accuracy":55,
        "fixTime":10,
        "source":"wifi",
        "geozones":{
        },
        "address":"xyz",
        "type":"location",
        "id":1121304719,
        "time":"2022-02-18T15:15:00+0000",
        "insertTime":"2022-02-18T15:46:55+0000",
        "seqNbr":1
    },
        {
        "state":"START",
        "lat":51.99225976,
        "lng":6.97070897,
        "accuracy":55,
        "fixTime":10,
        "source":"wifi",
        "geozones":{
        },
        "address":"xyz",
        "type":"location",
        "id":1121206955,
        "time":"2022-02-18T14:50:00+0000",
        "insertTime":"2022-02-18T14:50:16+0000",
        "seqNbr":0
    },
        {
        "state":"STOP",
        "lat":51.99225976,
        "lng":6.97070897,
        "accuracy":55,
        "fixTime":10,
        "source":"wifi",
        "geozones":{
        },
        "address":"xyz",
        "type":"location",
        "id":1121167953,
        "time":"2022-02-18T14:03:00+0000",
        "insertTime":"2022-02-18T14:34:54+0000",
        "seqNbr":15
    },
        ...
],
"truncated":false,
"skipped":false,
"serial":"H3HU7Y",
"name":"SNT3.5 H3HU7Y",
"type":"seri"
}
]

A simple approach is to use JArray and iterate through the values in the DOM, extracting them as needed:

var parsed = JArray.Parse(json);
foreach (var item in parsed[0]["data"])
{
    Console.WriteLine($"{item["lat"]} {item["lng"]} {item["fixTime"]}");
}

However, a more robust method is to deserialize into classes. I just pasted your json into https://json2csharp.com . It even suggests how to deserialize your json (although, since the json represents an array I had to update to deserialize to List<Root> ). This is the result (comments are mine):

// use this to deserialize
List<Root> myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);

// classes
// you'll need to define what goes here
public class Geozones
{
}

public class Datum
{
    public string state { get; set; }
    public double lat { get; set; }
    public double lng { get; set; }
    public int accuracy { get; set; }
    public int fixTime { get; set; }
    public string source { get; set; }
    public Geozones geozones { get; set; }
    public string address { get; set; }
    public string type { get; set; }
    public int id { get; set; }
    public DateTime time { get; set; }
    public DateTime insertTime { get; set; }
    public int seqNbr { get; set; }
}

public class Root
{
    public List<Datum> data { get; set; }
    public bool truncated { get; set; }
    public bool skipped { get; set; }
    public string serial { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}

Then to get the values:

foreach (var item in myDeserializedClass)
{
    foreach (var data in item.data)
    { 
        Console.WriteLine($"{data.lat} {data.lng} {data.fixTime}");
    }
}

Alternatively, you could mix the two approaches and use ToObject<T>() :

List<Datum> items = JArray.Parse(json)[0]["data"].ToObject<List<Datum>>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM