json - 如何在经典 ASP 中访问 Json 数组中的数据
问题描述
我的最终目标是从 json 创建一个动态菜单。我在到达儿童阵列时遇到问题。我可以从顶层层次结构中打印出文本,但在此之下什么也没有。我现在的目标是将所有文本字段从这个 json 打印到 html。
我对 ASP 经典没有经验,我整天都在努力解决这个问题。
这是 json 的简化版本。
[
{
"text":"Menüü",
"children":[
{
"text":"Helistamine"
},
{
"text":"Info"
},
{
"text":"Kontaktide nimekiri"
}
]
},
{
"text":"Admin",
"children":[
{
"text":"Kõik kirjed",
"children2":[
{
"text":"Vaata"
},
{
"text":"Muuda"
}
]
}
]
}
]
我正在使用ASP JSON 类。我收到错误:对象不是集合
这是理论上应该打印出孩子中所有数据的代码:
'Load JSON string'
oJSON.loadJSON(jsonString)
'Get single value'
Response.Write oJSON.data("text") & "<br>"
'Loop through collection'
For Each phonenr In oJSON.data("children")
Set this = oJSON.data("children").item(phonenr)
Response.Write _
this.item("text") & "<br>"
Next
我用不是数组的 json 尝试了相同的代码,它工作正常。如果有人能指出我正确的方向,我将不胜感激!
编辑:
我将json更改为:
{
"data":[
{
"text":"Menüü",
"children":[
{
"text":"Helistamine"
},
{
"text":"Info"
},
{
"text":"Kontaktide nimekiri"
}
]
},
{
"text":"Admin",
"children":[
{
"text":"Kõik kirjed",
"children2":[
{
"text":"Vaata"
},
{
"text":"Muuda"
}
]
}
]
}
]
}
我将代码更改为:
For Each x In oJSON.data("data")("children")
Set this = oJSON.data("data")("children").item(x)
response.Write this.item("text") & "<br>"
Next
但我仍然得到同样的错误对象不是集合。
编辑(2):
For Each x In oJSON.data("data")
Set this = oJSON.data("data").item(x)
response.Write this.item("text") & "<br>"
For Each y In x.data("children") 'need a replacement for x'
Set this = oJSON.data("children").item(y)
response.Write this.item("text") & "<br>"
Next
Next
需要对象:'0 - 我知道 x 是错误的
编辑 3(问题已解决):最后我可以访问 json 的子部分。(我将“edit”更改为“parent”,因为“edit”是系统变量)
'Loop through collection'
For Each x In oJSON.data("data")
set parent = oJSON.data("data").item(x)
response.Write parent.item("text") & "<br>"
For Each y In parent("children")
Set child = parent("children").item(y)
Response.Write child.item("text") & "<br>"
Next
Next
现在按预期打印:
'Menüü
Helistamine
Info
Kontaktide nimekiri
Admin
Kõik kirjed'
最后一个问题。我正在尝试使用相同的逻辑打印出孩子的孩子。该代码给出了错误对象而不是集合。
For Each x In oJSON.data("data")
set parent = oJSON.data("data").item(x)
response.Write parent.item("text") & "<br>"
For Each y In parent("children")
Set child = parent("children").item(y)
Response.Write child.item("text") & "<br>"
For Each z In child("children2")
Set grandchild = child("children2").item(z)
Response.Write grandchild.item("text") & "<br>"
Next
Next
Next
编辑4:这是我最新问题的解决方案
For Each x In oJSON.data("data")
set parent = oJSON.data("data").item(x)
response.Write parent.item("text") & " "
if isObject(parent("children")) then
For Each y In parent("children")
Set child = parent("children").item(y)
Response.Write child.item("text") & " "
if isObject(child("children2")) then
For Each z In child("children2")
Set grandchild = child("children2").item(z)
Response.Write grandchild.item("text") & "<br>"
Next
end if
Next
end if
Next
解决方案
推荐阅读
- html - '无法在 Nuxt.js 应用程序中播放音频文件'
- reactjs - ReactJS/Typescript Axios 获得 HTTP 支持的跨 CORS
- c++ - 如何将元组转换为初始化列表
- java - 如何使用 JPA Buddy 在多对多关系中自动创建关联(或联结)表
- solidity - 如何制作基于 APY% 输入的值
- ebay-api - eBay Sold URLS 中的 ?hash=item URL 参数是什么意思?
- kubernetes - 为什么删除 Kubernetes 命名空间需要这么长时间?
- python - 如何在 Networkx 图上添加箭头
- javascript - 还可以通过重置按钮更改自定义滑块的渐变
- google-sheets - *谷歌表*公式排序和删除重复