0

Using http://www.aspjson.com/ or https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp object I managed to get my data from URL to the dictionary object. But I tried and can not think of the way to get the data from the "opening" object using aspjson :-( I have not managed to find a way to get any data using json2.asp library. Here is my data:

{
"restaurant": {
    "id": 6,
    "email": "[email protected]",
    "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
}

}

I know both libraries use dictionary object to store the data, but I am lost how do I retrieve the data from the object.

1 Answer 1

4

You can use the isObject check to see if the element has inner members.

The include file used is from the link you gave

<!--#include file="aspJSON1.17.asp" -->
<%
Set oJSON = New aspJSON
jsonstring = "{ "&_
"""restaurant"": {"&_
    """id"": 6,"&_
    """email"": ""[email protected]"","&_
    """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




%>

Using the file at:https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp You can traverse through the json using the .enumerate() call, which returns all keys in a name-value collection and indexes in an array.

<%

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

%>

Call it like:

set oJSON= json.parse(jsonstring)
Traverse oJSON
Sign up to request clarification or add additional context in comments.

1 Comment

Works great, that is what I needed, I couldn't figure that out how to iterate through nested objects :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.