VB.NET需要帮助反序列化JSON

时间:2015-06-29 17:16:14

标签: json vb.net steam

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

-E

最后应该看起来像:

2722309060#2722173409#2721759518#2721748390

{
  "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
    }
  }
}

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

1 个答案:

答案 0 :(得分:0)

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