Cycling JSON List doesn't work [solved]

Just starting out? Need help? Post your questions and find answers here.
menschmarkus
Posts: 54
Joined: Thu Apr 10, 2014 3:35 pm

Cycling JSON List doesn't work [solved]

Post by menschmarkus »

Hi there,
I am testing SB in the moment and faced a problem with handling JSON string.
I call my Server with a PHP routine an get a valid JSON String. (See in code sample).
I tried to read it but while cycling throug the LIST ist stops after the first object. This is surprising me because I tested the same code in PB and it is working fine. (original code from english PB forum can be found here: https://www.purebasic.fr/english/viewto ... n+tutorial
I am wondering if there is a mistake in the code especially for SB

Code: Select all

EnableExplicit
#JSON = 0
Declare jsonArrayRetrieveElements(jsonObjectValue)
Declare jsonRetrieveMembers(jsonObjectValue,status)
Declare jsontest(result.s)

Define Query.s

Procedure HttpGetResult(Success, Result.s, UserData)
  If Success
    Debug Result
    jsontest(Result)
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  
;{ JSON Procedures
Procedure jsonArrayRetrieveElements(jsonObjectValue)
 Protected jsonMemberKeyName.s,jArraySize.i,jsonArrayElement.i,jsonValueType.i,i.i
  jsonMemberKeyName.s = JSONMemberKey(jsonObjectValue)
  jArraySize = JSONArraySize(JSONMemberValue(jsonObjectValue))
  For i = 0 To (jArraySize - 1)
   
    jsonArrayElement = GetJSONElement(JSONMemberValue(jsonObjectValue), i)
    jsonValueType = JSONType(jsonArrayElement)         
   
    If jsonValueType = #PB_JSON_String
     
      Debug " > array element " + Str(i) + " = " + GetJSONString(jsonArrayElement)
     
    ElseIf jsonValueType = #PB_JSON_Number
     
      Debug " > array element " + Str(i) + " = " + Str(GetJSONDouble(jsonArrayElement))
     
    ElseIf jsonValueType = #PB_JSON_Array
     
      Debug " > array element " + Str(i) + " = JSON Array"
      jsonArrayRetrieveElements(jsonArrayElement)
     
    ElseIf jsonValueType = #PB_JSON_Object
     
      Debug " > array element " + Str(i) + " = JSON Object"           
      jsonRetrieveMembers(jsonArrayElement,1)
     
    EndIf
   
  Next
 
EndProcedure

Procedure jsonRetrieveMembers(jsonObjectValue,status)
  Protected jsonValueType.i,jsonMemberKeyName.s, name1.s,name2.s,name3.s,name4.s,name5.s,name6.s,address1.s,address2.s,source.s, user.s, owner.s, sstatus.i, kontingent.i, name.s, adresse.s
  ; retrieve the json object members
  If ExamineJSONMembers(jsonObjectValue)
    ; iterate through the json object members
    While NextJSONMember(jsonObjectValue)
     
      ; determine the type of value stored in the member
      jsonValueType = JSONType(JSONMemberValue(jsonObjectValue))
     
      ; retrieve the key name of the member
      jsonMemberKeyName.s = JSONMemberKey(jsonObjectValue)
      If status = 0
        Select jsonMemberKeyName
          Case "USER"
            user = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Owner"
            owner = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "STATUS"
            sstatus = GetJSONInteger(JSONMemberValue(jsonObjectValue))
          Case "Restkontingent"
            kontingent = GetJSONInteger(JSONMemberValue(jsonObjectValue))
          Case "SEARCHNAME"
            name = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "SEARCHADRESS"
            adresse = GetJSONString(JSONMemberValue(jsonObjectValue))
        EndSelect
      ElseIf status = 1
        Select jsonMemberKeyName
          Case "Name1"
            name1 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Name2"
            name2 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Name3"
            name3 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Name4"
            name4 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Name5"
            name5 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Name6"
            name6 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Adresse1"
            address1 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Adresse2"
            address2 = GetJSONString(JSONMemberValue(jsonObjectValue))
          Case "Listenherkunft"
            source = GetJSONString(JSONMemberValue(jsonObjectValue))
        EndSelect
      EndIf
      
      
      ; retrieve the key-value of each member according
      ; to the respective data type and display them
      If jsonValueType = #PB_JSON_String
       
        ; retrieve & display the string value of the member
        Debug " " + jsonMemberKeyName + " = " +
              GetJSONString(JSONMemberValue(jsonObjectValue))
       
      ElseIf jsonValueType = #PB_JSON_Number
       
        ; retrieve & display the numeric value of the member
        Debug " " + jsonMemberKeyName + " = " +
              GetJSONDouble(JSONMemberValue(jsonObjectValue))       
       
      ElseIf jsonValueType = #PB_JSON_Object
       
        Debug " " + jsonMemberKeyName + " = JSON Object"
        jsonRetrieveMembers(JSONMemberValue(jsonObjectValue),1)
       
      ElseIf jsonValueType = #PB_JSON_Array
       
        jsonArrayRetrieveElements(jsonObjectValue)
      EndIf     
     
    Wend
   
  EndIf
 
EndProcedure

Procedure jsontest(jsnstring.s)
  Protected jsonObjectValue.i
  If ParseJSON(#JSON,jsnstring)
    jsonObjectValue = JSONValue(#JSON)
    jsonRetrieveMembers(jsonObjectValue,0)
  Else
    MessageRequester("Konnte JSON Datei nicht laden" + Chr(10) + JSONErrorLine() + Chr(10) + JSONErrorMessage() + Chr(10) + JSONErrorPosition())
  EndIf  
EndProcedure

Query.s = "{" + Chr(34) + "Owner" + Chr(34) + ":"+ Chr(34) + "1234567890" + Chr(34) + "," + Chr(34) + "USER" + Chr(34) + ":"+ Chr(34) + "0987654321" + Chr(34) + ","+ Chr(34) + "SEARCHNAME" + Chr(34) + ":" + Chr(34) + "<searchname>" + Chr(34) + "," + Chr(34) + "SEARCHADRESS" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "STATUS" + Chr(34) + ":0,"+ Chr(34) + "Restkontingent" + Chr(34) + ":249676," + Chr(34) + "RESULT" + Chr(34) + ":[{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 1.0>, <Ergebnisname 1.1>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) +  Chr(34) + "," + Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.1.0>, <Ergebnisname 2.1.1>" + Chr(34) + "," + Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.2.0>" + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisadresse 2.1.0>" + Chr(34) + ","+ Chr(34) + "Adresse2"+ Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft 3>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 3.1.0>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "}]}"
HttpGetResult(1,Query,0)

;}
Hopefully someone can help me.

thanks for your useful comments
Last edited by menschmarkus on Sat Jun 13, 2020 9:32 am, edited 1 time in total.
as soon you do it right, it works !
User avatar
Paul
Posts: 215
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Cycling JSON List doesn't work

Post by Paul »

This doesn't answer your original question but if you are looking for a clean and easy way to work with JSON data I would suggest using Structures.
This method has worked great for what I do and makes the code a lot easier to follow.

Code: Select all

Query.s = "{" + Chr(34) + "Owner" + Chr(34) + ":"+ Chr(34) + "1234567890" + Chr(34) + "," + Chr(34) + "USER" + Chr(34) + ":"+ Chr(34) + "0987654321" + Chr(34) + ","+ Chr(34) + "SEARCHNAME" + Chr(34) + ":" + Chr(34) + "<searchname>" + Chr(34) + "," + Chr(34) + "SEARCHADRESS" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "STATUS" + Chr(34) + ":0,"+ Chr(34) + "Restkontingent" + Chr(34) + ":249676," + Chr(34) + "RESULT" + Chr(34) + ":[{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 1.0>, <Ergebnisname 1.1>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) +  Chr(34) + "," + Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.1.0>, <Ergebnisname 2.1.1>" + Chr(34) + "," + Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.2.0>" + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisadresse 2.1.0>" + Chr(34) + ","+ Chr(34) + "Adresse2"+ Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft 3>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 3.1.0>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "}]}"


Structure ResultData
  Listenherkunft.s
  Name1.s
  Name2.s
  Name3.s
  Name4.s
  Name5.s
  Name6.s
  Address1.s
  Address2.s
EndStructure

Structure jsondata
  Owner.s
  USER.s
  SEARCHNAME.s
  SEARCHADDRESS.s
  STATUS.l
  Restkontingent.l
  List RESULT.ResultData()
EndStructure


hJSON= ParseJSON(#PB_Any, Query)
If hJSON
  ExtractJSONStructure(JSONValue(hJSON), @json.jsondata, jsondata)            
  FreeJSON(hJSON)
EndIf


Debug "Owner: "+json\Owner
Debug "User: "+json\USER
Debug "SearchName: "+json\SEARCHNAME
Debug "SearchAddress: "+json\SEARCHADDRESS
Debug "Status: "+json\STATUS
Debug "Restkontingent: "+json\Restkontingent
Debug "=============="
        
ForEach json\RESULT()
  Debug "Listenherkunft: "+json\RESULT()\Listenherkunft
  Debug "Name1: "+json\RESULT()\Name1
  Debug "Name2: "+json\RESULT()\Name2
  Debug "Name3: "+json\RESULT()\Name3
  Debug "Name4: "+json\RESULT()\Name4
  Debug "Name5: "+json\RESULT()\Name5
  Debug "Name6: "+json\RESULT()\Name6
  Debug "Address1: "+json\RESULT()\Address1
  Debug "Address2: "+json\RESULT()\Address2
  Debug "---"
Next 
User avatar
Peter
Posts: 1206
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Cycling JSON List doesn't work

Post by Peter »

Argh! Paul beat me, but I still answer. ;)

@menschmarkus: You try to execute a GetJSONString() on an array at one point. I don't have the time to analyze this in detail at the moment.

However, I would suggest that you use the much easier to use and more reliable ExtractJSONStructure().

Code: Select all

EnableExplicit

Structure sResult
  Listenherkunft.s
  Name1.s
  Name2.s
  Name3.s
  Name4.s
  Name5.s
  Name6.s
  Adresse1.s
  Adresse2.s
EndStructure

Structure sItem
  
  Owner.s
  USER.s
  SEARCHNAME.s
  SEARCHADRESS.s
  STATUS.i
  Restkontingent.i
  List RESULT.sResult()
  
EndStructure

#JSON = 0
Define Query.s
Define Item.sItem

Query.s = "{" + Chr(34) + "Owner" + Chr(34) + ":"+ Chr(34) + "1234567890" + Chr(34) + "," + Chr(34) + "USER" + Chr(34) + ":"+ Chr(34) + "0987654321" + Chr(34) + ","+ Chr(34) + "SEARCHNAME" + Chr(34) + ":" + Chr(34) + "<searchname>" + Chr(34) + "," + Chr(34) + "SEARCHADRESS" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "STATUS" + Chr(34) + ":0,"+ Chr(34) + "Restkontingent" + Chr(34) + ":249676," + Chr(34) + "RESULT" + Chr(34) + ":[{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 1.0>, <Ergebnisname 1.1>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) +  Chr(34) + "," + Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) +  Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.1.0>, <Ergebnisname 2.1.1>" + Chr(34) + "," + Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 2.2.0>" + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisadresse 2.1.0>" + Chr(34) + ","+ Chr(34) + "Adresse2"+ Chr(34) + ":"+ Chr(34) + Chr(34) + "},{" + Chr(34) + "Listenherkunft" + Chr(34) + ":"+ Chr(34) + "<Listenherkunft 3>" + Chr(34) + ","+ Chr(34) + "Name1" + Chr(34) + ":"+ Chr(34) +  "<Ergebnisname 3.1.0>" + Chr(34) + ","+ Chr(34) + "Name2" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name3" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name4" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name5" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Name6" + Chr(34) + ":"+ Chr(34) + Chr(34) + ","+ Chr(34) + "Adresse1" + Chr(34) + ":"+ Chr(34) + Chr(34) + "," + Chr(34) + "Adresse2" + Chr(34) + ":"+ Chr(34) + Chr(34) + "}]}"

ParseJSON(#JSON,Query)
  
ExtractJSONStructure(JSONValue(#JSON), @Item, sItem)
  
Debug "Owner: " + Item\Owner
Debug "USER: " + Item\USER
Debug "SEARCHNAME: " + Item\SEARCHNAME
Debug "SEARCHADRESS: " + Item\SEARCHADRESS
Debug "STATUS: " + Item\STATUS
Debug "Restkontingent: " + Item\Restkontingent

ForEach Item\RESULT()
  Debug "Result(" + ListIndex(Item\RESULT()) + "): " + Item\RESULT()\Listenherkunft
  Debug "Result(" + ListIndex(Item\RESULT()) + "): " + Item\RESULT()\Name1
  Debug "and so on..."
  Debug ""
Next
menschmarkus
Posts: 54
Joined: Thu Apr 10, 2014 3:35 pm

Re: Cycling JSON List doesn't work

Post by menschmarkus »

@Peter and Paul
Thanks for your fast replies. Due to the fact these are my first steps with JSON I tried the uggly way first to understand what happens.
Using structures was my aim. Great, you boosted me forward with your code :mrgreen: .
Thank you again.
In the meantime I debugged the code in my browser and found, that the recursive call to the second cycle of the list the JSONObject is not longer defined. It seems this is a bug in SB. Fred may have a look through this to check it.

I will implement your code and will close the post after success.
as soon you do it right, it works !
Post Reply