经典 ASP JSON 子 collection

Classic ASP JSON sub collection

我正在使用来自 http://www.aspjson.com/ 的经典 ASP JSON class 来转换来自外部来源的 JSON 提要,以便在我的站点中使用。

对于单个级别 collection 我做得很好。

JSON 看起来像这样:

{
   "data":[
      {
         "message":"message 5",
         "id":"5"
      },
      {
         "message":"message 4",
         "id":"4"
      },
      {
         "message":"message 3",
         "id":"3"
      },
      {
         "message":"message 2",
         "id":"2"
      },
      {
         "message":"message 1",
         "id":"1"
      }
   ]
}

并且代码像这样工作得很好:

TheFeed = [url of external json feed]
Set objXML=Server.CreateObject("Microsoft.XMLHTTP")
objXML.Open "GET", TheFeed, False
objXML.Send
strContents=objXML.ResponseText
Set objXML=Nothing

Set oJSON = New aspJSON
oJSON.loadJSON(strContents)

For Each i In oJSON.data("data")
    Set this = oJSON.data("data").item(i)
    response.write this.item("message")
end if
next

然而,现在提要包含更多信息,其中一些位于顶级 collection 的 sub-collections 中,我真的很难弄清楚如何访问它,使用data/item 个可供我选择的选项。可能是我做不到我想做的事,如果有人能证实这一点,我将不胜感激,但我假设更有可能是我的代码不正确。

所以我的更复杂的提要看起来更像这样(因为这是外部提要,我无权控制 JSON 是如何产生的,我只能使用我得到的东西):

{
   "data":[
      {
         "message":"message 5",
         "id":"5"
      },
      {
         "message":"message 4",
         "id":"4"
      },
      {
         "message":"message 3",
         "id":"3"
      },
      {
         "message":"message 2",
         "id":"2",
         "attachments":{
            "data":[
               {
                  "subattachments":{
                     "data":[
                        {
                           "media":{
                              "image":{
                                 "src":"1.jpg"
                              }
                           },
                           "type":"photo"
                        },
                        {
                           "media":{
                              "image":{
                                 "src":"2.jpg"
                              }
                           },
                           "type":"photo"
                        },
                        {
                           "media":{
                              "image":{
                                 "src":"3.jpg"
                              }
                           },
                           "type":"photo"
                        }
                     ]
                  }
               }
            ]
         }
      },
      {
         "message":"message 1",
         "id":"1"
      }
   ]
}

我真正想做的是在我的初始循环中有一个辅助循环来浏览那些照片附件。但我只是不确定如何到达 JSON 的正确位来循环。

到目前为止,我所做的一切都没有对我造成错误:

If Not IsEmpty(this.item("attachments")) Then
  for each i in this.item("attachments")
   set this2 = this.item("attachments").item(i)
   response.write 'here'
 next
end if

仅在消息 2 上输出单个 'here',这是我所期望的,但我似乎无法做任何进一步的事情。我尝试使用 "subattachments" 或任何其他低于 "attachment" 级别的东西都告诉我 "Object not a collection".

如果有人能指出我做错了什么,我将不胜感激。

我没有充分使用 'data' 项目。

此代码:

If Not IsEmpty(this.item("attachments")) Then
 for each i in this.item("attachments").item("data").item(0).item("subattachments").item("data")
  set this2 = this.item("attachments").item("data").item(0).item("subattachments").item("data").item(i)
  response.write "here"
 next
end if

Returns 我所希望的 "here" 倍数。我也可以在一个循环中完成一个循环,但由于第一个集合中只有一个项目,我只是使用 0 索引引用它。

我现在应该可以毫无问题地从他的子附件中找出我需要的物品。