【问题标题】:How to access JSON data in classic ASP using json2.asp or aspjson libraries?如何使用 json2.asp 或 aspjson 库访问经典 ASP 中的 JSON 数据?
【发布时间】:2014-07-26 18:45:46
【问题描述】:

使用http://www.aspjson.com/https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp 对象,我设法将我的数据从URL 获取到字典对象。但是我尝试并想不出使用aspjson从“开放”对象获取数据的方法:-(我还没有设法找到使用json2.asp库获取任何数据的方法。这是我的数据:

{
"restaurant": {
    "id": 6,
    "email": "xyz@gmail.com",
    "visiblemail": "1",
    "date": "2014-07-24 07:38:59",
    "logo": "818_294.png",
    "img": "818_554|818_558|818_563",
    "opening": {
        "sun": [
            "closed"
        ],
        "mon": [
            "10.00",
            "20.00"
        ],
        "tue": [
            "10.00",
            "20.00"
        ],
        "wed": [
            "10.00",
            "20.00"
        ],
        "thu": [
            "10.00",
            "20.00"
        ],
        "fri": [
            "10.00",
            "20.00"
        ],
        "sat": [
            "closed"
        ],
        "hol": [
            "zaprto"
        ]
    },

    "timetable": null
}

}

我知道这两个库都使用字典对象来存储数据,但我不知道如何从对象中检索数据。

【问题讨论】:

    标签: json asp-classic


    【解决方案1】:

    您可以使用isObject 检查元素是否有内部成员。

    使用的包含文件来自您提供的链接

    <!--#include file="aspJSON1.17.asp" -->
    <%
    Set oJSON = New aspJSON
    jsonstring = "{ "&_
    """restaurant"": {"&_
        """id"": 6,"&_
        """email"": ""xyz@gmail.com"","&_
        """visiblemail"": ""1"","&_
        """date"": ""2014-07-24 07:38:59"","&_
        """logo"": ""818_294.png"","&_
        """img"": ""818_554|818_558|818_563"","&_
        """opening"": {"&_
        "    ""sun"": ["&_
        "        ""closed"""&_
        "    ],"&_
        "    ""mon"": ["&_
        "        ""10.00"","&_
        "        ""20.00"""&_
        "    ],"&_
        "    ""tue"": ["&_
        "        ""10.00"","&_
        "        ""20.00"""&_
        "    ],"&_
        "    ""wed"": ["&_
        "        ""10.00"","&_
        "        ""20.00"""&_
        "    ],"&_
        "    ""thu"": ["&_
        "        ""10.00"","&_
        "        ""20.00"""&_
        "    ],"&_
        "    ""fri"": ["&_
        "        ""10.00"","&_
        "        ""20.00"""&_
        "    ],"&_
        "    ""sat"": ["&_
        "        ""closed"""&_
        "    ],"&_
        "    ""hol"": ["&_
        "        ""zaprto"""&_
        "    ]"&_
        "},"&_
        """timetable"": null"&_
    "}"
    
    
    'Load JSON string
    oJSON.loadJSON(jsonstring)
    
    set restaurant = oJSON.data("restaurant")
    
    for each itm in restaurant
        if Not IsObject(restaurant.item(itm)) then
            Response.write itm  &" : "& restaurant.item(itm) & "<br/>"
        else
        'opening
            for each dayy in restaurant.item(itm)
                Response.write dayy & ":"
                    Response.write restaurant.item(itm)(dayy)(0) 
    
                    If restaurant.item(itm)(dayy)(1) <> "" Then
                        Response.write " - "
                        Response.write restaurant.item(itm)(dayy)(1) 
                    End If
    
                Response.write "<br/>"
            next
        end if
    
        
    next
    
    
    
    
    %>
    

    使用文件地址:https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp 您可以使用 .enumerate() 调用遍历 json,该调用返回名称-值集合中的所有键和数组中的索引。

    <%
    
    Sub Traverse(oJson)
        Dim key
    
        For Each key In oJson.enumerate()
            
            If IsObject(oJson.get(key)) Then
                Response.write key & " => " 
                Traverse oJson.get(key) 'Recursive call
            Else
                Response.Write(key & "=" & oJson.get(key) & "<br/>")
            End If
        Next
    
    End Sub
    
    %>
    

    这样称呼:

    set oJSON= json.parse(jsonstring)
    Traverse oJSON
    

    【讨论】:

    • 效果很好,这正是我所需要的,我不知道如何迭代嵌套对象:-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多