将嵌套 Json 反序列化为列表
Deserialize nested Json into a list
我想反序列化以下嵌套的 json。为此,我需要数据:- state, lat, lng, time - 从 json 到列表中。在 Newtonsoft 网站上,我找不到任何对我有帮助的信息。有谁知道如何反序列化这个 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"
}
]
一个简单的方法是使用 JArray
并遍历 DOM 中的值,根据需要提取它们:
var parsed = JArray.Parse(json);
foreach (var item in parsed[0]["data"])
{
Console.WriteLine($"{item["lat"]} {item["lng"]} {item["fixTime"]}");
}
不过,更健壮的方法是反序列化为类。我刚刚将你的 json 粘贴到 https://json2csharp.com 中。它甚至建议如何反序列化您的 json(尽管,由于 json 代表一个数组,我必须更新以反序列化为 List<Root>
)。这是结果(评论是我的):
// 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; }
}
然后获取值:
foreach (var item in myDeserializedClass)
{
foreach (var data in item.data)
{
Console.WriteLine($"{data.lat} {data.lng} {data.fixTime}");
}
}
或者,您可以混合使用这两种方法并使用 ToObject<T>()
:
List<Datum> items = JArray.Parse(json)[0]["data"].ToObject<List<Datum>>();
我想反序列化以下嵌套的 json。为此,我需要数据:- state, lat, lng, time - 从 json 到列表中。在 Newtonsoft 网站上,我找不到任何对我有帮助的信息。有谁知道如何反序列化这个 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"
}
]
一个简单的方法是使用 JArray
并遍历 DOM 中的值,根据需要提取它们:
var parsed = JArray.Parse(json);
foreach (var item in parsed[0]["data"])
{
Console.WriteLine($"{item["lat"]} {item["lng"]} {item["fixTime"]}");
}
不过,更健壮的方法是反序列化为类。我刚刚将你的 json 粘贴到 https://json2csharp.com 中。它甚至建议如何反序列化您的 json(尽管,由于 json 代表一个数组,我必须更新以反序列化为 List<Root>
)。这是结果(评论是我的):
// 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; }
}
然后获取值:
foreach (var item in myDeserializedClass)
{
foreach (var data in item.data)
{
Console.WriteLine($"{data.lat} {data.lng} {data.fixTime}");
}
}
或者,您可以混合使用这两种方法并使用 ToObject<T>()
:
List<Datum> items = JArray.Parse(json)[0]["data"].ToObject<List<Datum>>();