首页 > 解决方案 > ASPjson 中的嵌套数组

问题描述

使用https://www.aspjson.com/解析写入 JSON

我有一个组织的数据结构。这些信息之一是他们的物理位置。每个位置可以有多个房间。但是,当我尝试编写代码以在每个位置项中创建房间时,我收到一个错误:Object required: 'JSON.data(...).

这是我迄今为止成功编写的代码:

{
  "locations": [{
      "locationid": 524,
      "locationname": "test building"
    },
    {
      "locationid": 525,
      "locationname": " building 1"
    },
    {
      "locationid": 526,
      "locationname": "test building 2"
    },
    {
      "locationid": 527,
      "locationname": "test building 3"
    },
  ]
}

我想做的是在每个位置项中创建一个“房间”集合,就像下面这样,但是在尝试创建第二个嵌套房间数组时出现上述错误。(见下面我的代码)

    {
      "locationid": 527,
      "locationname": "test building 3",
      "rooms" :[
        {
          "roomid": 111,
          "roomname": "room 1 "
        },
        {
          "roomid": 222,
          "roomname": "room 2"
        },
      ]
    },

// additional locations follow ...

我当前的代码:

          .Add "locations", JSON.Collection()
          With JSON.data("locations")

                 .Add 0, JSON.Collection()
                 With .item(0)
                       .Add "locationid", 111
                       .Add "locationname", "location 1"

                       .Add "rooms", JSON.Collection()
                       With JSON.data("rooms")  /// this is where I get the error. 

             .Add 0, JSON.Collection()
              With .item(0)
                  .Add "roomid",  1
                  .Add "roomname", "room1"
             end with 

             .Add 1, JSON.Collection()
              With .item(1)
                  .Add "roomid",  2
                  .Add "roomname", "room2"
              end with 



                       end with
                 end with
          
          end with

标签: jsonvbscript

解决方案


您需要遵循的对象的结构JSON.data("rooms")将不存在,因为JSON.data()是从 JSON 结构的根读取。添加rooms集合后,您需要从该实例开始工作,因为当前.Item实例是添加集合的上下文rooms。所以这条线应该是;

With .Item("rooms")

推荐阅读