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”?

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" 为 false 时,结果项目列表将为空。测试它是否有效:

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

结果:

2722309060
2722173409
2721759518
2721748390