rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
204 stars 89 forks source link

Deserializing JSON #39

Closed zharris6 closed 7 years ago

zharris6 commented 7 years ago

Hey rcdmk,

Thanks in advance for your help.

I am trying to deserialize a JSON object. I was able to run and parse the string like the below example.

My question is how to I loop over each value of this object?

I have tried your examples with regards to putting it in an array, then looping..but haven't had any luck.

set JSON = New JSONobject       

JSON.Parse(jsonstring)

 Json.Write()

The Output is :

{"data":[{"Guid":"05409efb-fa5d-4bd5-81e6-b68c85b7754a","Date":"2017-02-27T15:04:07.333","Key":"scrapePrefix","Value":"www","Type":"String"},{"Guid":"05409efb-fa5d-4bd5-81e6-b68c85b7754a","Date":"2017-02-27T15:04:07.333","Key":"scrapePostfix","Value":"local.com","Type":"String"}]}

zharris6 commented 7 years ago

I was able to do some Investigation by debugging the object and was able to do it like so. Is this the best day to iterate through the collection?

    set JSON = New JSONobject
       JSON.Parse(SessionValue)

        for each item in JSON.pairs

            for each item2 in item.i_value.items

                Response.Write " Key: " & item2("Key")
                Response.Write " Value: " & item2("Value")
                Response.Write " Type: " & item2("Type") & "<br>"

            next

        next
rcdmk commented 7 years ago

Hi,

You are almost there. When you parse an array like string it will generate and return a JSONArr object that is referenced in the property "data" in this case.

<%
'...
Dim JSON, arr, item

Set JSON = New JSONobject
Set arr = JSON.Parse(SessionValue)

For Each item In arr.items
    Response.Write " Key: " & item("Key")
    Response.Write " Value: " & item("Value")
    Response.Write " Type: " & item("Type") & "<br>"
Next
%>

You can see more examples at the bottom of the README page (project home page).

zharris6 commented 7 years ago

Works Perfect, Cheers!!