gpt4 book ai didi

json - 如何在另一个 JObject 中为动态 JObject 循环?

转载 作者:行者123 更新时间:2023-12-04 08:29:09 29 4
gpt4 key购买 nike

实际上,我正在处理从 Framework 3.5 中的旧 VB.NET 项目中的某个 API 获取的一些 JSON 数据
我被困在以下响应的处理中:

{
"data": {
"89359": {
"name": "A",
//...
},
"89360": {
"name": "B",
//...
}
}
}
数据中的每个项目都是动态的,但 API 没有将数据设置为数组,我试图从每个项目中获取数据,就像我对 JSON 数组所做的那样:
Dim options As JObject = JObject.Parse(json)
For Each opt As JObject In options("data")
MsgBox(opt("name"))
Next
但在这种情况下它不起作用,因为我说数据不是数组......
那么如何处理来自 JSON 的数据,因为 89359 和 89360 是动态的?

最佳答案

你的问题是 options("data")被声明为类型 JToken 而实际上是类型 JObject ,所以当你枚举它时,你实际上是在枚举它的 JProperty children 。但您需要的是 values其中JProperty在层次结构中更深一层的对象:

For Each jProperty As JProperty In options("data")
Dim propertyName = jProperty.Name ' The "89359" names, in case you need them for some reason
Dim opt As JObject = jProperty.Value
Dim name As String = opt("name") ' Cast the name to a String using the JToken implicit casting operator
' Use the name as required
MsgBox(name)
Next
演示 fiddle here .

关于json - 如何在另一个 JObject 中为动态 JObject 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65110846/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com