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)
;}thanks for your useful comments
