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

Problem with JSONArray and JSON Object #35

Closed Wizard2k closed 7 years ago

Wizard2k commented 7 years ago

Hi guys, I have a problem using JSON array, or better open a serialized JSON Array

This is the story: A procedure load data into json object like this:

SQL = "select IdUser, Name from admuser where iduser=1 Or iduser = 2"
set recdati = Server.createObject("adodb.recordset")
recdati.open sql, mconn 
if not (recdati.eof and recdati.bof) then
    Set Prova = New JSONarray
    Call Prova.LoadRecordset(recDati)
        sString = Prova.Serialize()
    set Prova = Nothing
End If

recDati.Close
Set recDati = Nothing

If I do a Response.Write of sString is:

[{"IdUser":1,"Name":"Aldo"},{"IdUser":2,"Name":"Manuela"}]

Now I try to load this Json into a new Json object:

Set myJson = New JSONObject
myJson.parse(sString)

Set arrJson = New JSONArray
arrJson.push myJson

for each item in arrJson.items
    if isObject(item) and typeName(item) = "JSONobject" then

        Response.Write "<br>item " & item.value("IdUser")
    else
        response.write item
    end if

    response.write "<br>"
next

My question is: How can I load an JSON Array and loop into it? The for each loop provide me nothing (and also seems that only one time it goes into loop)

Another thing: If I do a myJson.Write the class print this:

{"data":[{"IdUser":1,"Name":"Aldo"},{"IdUser":2,"Name":"Manuela"}]}

Adding "data" before the json array

Can anyone help me?

rcdmk commented 7 years ago

Hi.

When you parse the string that represents an array into a json object, the parse method returns the array instance that is created in the neon object.

You don't need to push it to a new JSON array.

<%
'...
Set arrJson = myJson.parse(sString)

for each item in arrJson.items
    if isObject(item) and typeName(item) = "JSONobject" then

        Response.Write "<br>item " & item.value("IdUser")
    else
        response.write item
    end if

    response.write "<br>"
next
%>
Wizard2k commented 7 years ago

Hi Ricardo! It works! Thank you very much!