VB.NET需要帮助反序列化JSON(VB.NET need help in deserialization of a JSON)

编程入门 行业动态 更新时间:2024-10-27 05:34:34
VB.NET需要帮助反序列化JSON(VB.NET need help in deserialization of a JSON)

嘿,我需要帮助反序列化:

{ "success": true, "rgInventory": { "2722309060": { "id": "2722309060", "classid": "939801430", "instanceid": "188530139", "amount": "1", "pos": 1 }, "2722173409": { "id": "2722173409", "classid": "937254203", "instanceid": "188530139", "amount": "1", "pos": 2 }, "2721759518": { "id": "2721759518", "classid": "720293857", "instanceid": "188530139", "amount": "1", "pos": 3 }, "2721748390": { "id": "2721748390", "classid": "310777652", "instanceid": "480085569", "amount": "1", "pos": 4 } } }

最后它应该看起来像:

2722309060#2722173409#2721759518#2721748390

Dim result = JsonConvert.DeserializeObject(jsonstring) 'deserialize it Dim tempfo As String = result("rgInventory").ToString 'get rgInventory Console.WriteLine(tempfo)

我如何反序化所有'id'?

Hey I need help in deserializing this:

{ "success": true, "rgInventory": { "2722309060": { "id": "2722309060", "classid": "939801430", "instanceid": "188530139", "amount": "1", "pos": 1 }, "2722173409": { "id": "2722173409", "classid": "937254203", "instanceid": "188530139", "amount": "1", "pos": 2 }, "2721759518": { "id": "2721759518", "classid": "720293857", "instanceid": "188530139", "amount": "1", "pos": 3 }, "2721748390": { "id": "2721748390", "classid": "310777652", "instanceid": "480085569", "amount": "1", "pos": 4 } } }

at the end it should look like:

2722309060#2722173409#2721759518#2721748390

Dim result = JsonConvert.DeserializeObject(jsonstring) 'deserialize it Dim tempfo As String = result("rgInventory").ToString 'get rgInventory Console.WriteLine(tempfo)

how i can deserialize all 'id's?

最满意答案

json包含项目词典,您想要的ID是键。 如果你反序列化它,你可以从字典中获取它们。 否则,您可以使用JParse和linq来获取它们:

Dim jstr As String = ...
' parse the json
Dim js As JObject = JObject.Parse(jstr)

' extract the inventory items
Dim ji As JObject = JObject.Parse(js("rgInventory").ToString)
' get and store the keys
Dim theIds As List(Of String) = ji.Properties.Select(Function(k) k.Name).ToList()
 

我怀疑当"success"为假时,结果项目列表将为空。 测试它的工作原理:

For Each s As String In theIds
    Console.WriteLine(s)
Next
 

结果:

2722309060 2722173409 2721759518 2721748390

The json contains a Dictionary of items, the IDs you want are the keys. If you deserialized it, you could get them from the Dictionary. Otherwise, you can use JParse and linq to get them:

Dim jstr As String = ...
' parse the json
Dim js As JObject = JObject.Parse(jstr)

' extract the inventory items
Dim ji As JObject = JObject.Parse(js("rgInventory").ToString)
' get and store the keys
Dim theIds As List(Of String) = ji.Properties.Select(Function(k) k.Name).ToList()
 

I suspect that when "success" is false that the resulting items list will be empty. Test that it works:

For Each s As String In theIds
    Console.WriteLine(s)
Next
 

Result:

2722309060 2722173409 2721759518 2721748390

更多推荐

本文发布于:2023-07-09 00:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   NET   VB   JSON   deserialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!