【问题标题】:VB.NET need help in deserialization of a JSONVB.NET 在 JSON 的反序列化方面需要帮助
【发布时间】:2015-06-29 17:16:13
【问题描述】:

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

{
  "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's?

【问题讨论】:

  • 不,那里的数据比那些 ID/键多得多。代码 sn-p 的结果是什么?它怎么不工作?您的实际问题是什么?
  • 问题是我如何反序列化所有 'id's

标签: json vb.net steam


【解决方案1】:

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

【讨论】:

  • 请注意,这实际上并没有反序列化 json(根据标题),它只是提取了一些位
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2010-12-15
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多