首页 > 解决方案 > 如何在经典 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

标签: jsonasp-classic

解决方案


推荐阅读