Code: Select all
Declare.s GetJsonAnyValue(value)
Declare.s HTTPJsonRequest(posttype,string.s,gegevens.s="")
Declare ExtractJsonObject(value)
Declare ExtractJSONArrayValues(value)
Global json.l
Procedure.s GetJSONAnyValue(Value)
Select JSONType(Value)
Case #PB_JSON_Null: ProcedureReturn "null"
Case #PB_JSON_String: ProcedureReturn GetJSONString(Value)
Case #PB_JSON_Number: ProcedureReturn StrD(GetJSONDouble(Value))
Case #PB_JSON_Boolean: ProcedureReturn Str(GetJSONBoolean(Value))
Case #PB_JSON_Array: ProcedureReturn "array"
Case #PB_JSON_Object: ProcedureReturn "object"
EndSelect
EndProcedure
Procedure.s HTTPJsonRequest(posttype,string.s,gegevens.s="")
NewMap Header.s()
HTTPRequest=HTTPRequest(posttype,string.s,gegevens,0,header())
If HttpRequest
HTTPStatuscode.s=HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
HTTPJSONResponse.s=HTTPInfo(HTTPRequest, #PB_HTTP_Response)
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
If HTTPStatuscode="200"
JSON=ParseJSON(#PB_Any,HTTPJSONResponse)
If json
ProcedureReturn ComposeJSON(json)
Else
ProcedureReturn "NOJSONRESPONSE"
EndIf
Else
ProcedureReturn HTTPStatuscode
EndIf
EndProcedure
Procedure ExtractJsonObject(value) ; this is an object...
If ExamineJSONMembers(Value)
While NextJSONMember(value)
Debug JSONMemberKey(value)+":"+GetJSONAnyValue(JSONMemberValue(value))
If GetJsonAnyValue(JSONMemberValue(value))="object"
Debug "object found -------------------------------- "
ExtractJsonObject(JSONMemberValue(value))
Debug "End of object ------------------------------- "
EndIf
If GetJsonAnyValue(JSONMemberValue(value))="array"
Debug "Array found --------------------------------- "
;ExtractJSONArrayValues(JSONMemberValue(value))
Debug "End of array -------------------------------- "
EndIf
Wend
EndIf
EndProcedure
Procedure ExtractJSONArrayValues(value) ; value is a jsonarray
Debug "Arraysize:"+Str(JSONArraySize(JSONValue(value)))
For teller=0 To JSONArraySize(JSONValue(value))-1
Debug "teller:"+Str(teller)
newvalue=GetJSONElement(JSONValue(value),teller)
ExtractJsonObject(newvalue)
Next teller
EndProcedure
InitNetwork()
APIkey.s= "apikeyverylongstring"
Debug HTTPJsonRequest(#PB_HTTP_Get,"https://user:"+apikey.s+"@api.printnode.com/printers")
ExtractJSONArrayValues(json)
If IsJSON(json)
FreeJSON(json)
EndIf
Code: Select all
[{"default":false,"state":"online","createTimestamp":"2020-12-24T12:29:02.619Z","computer":{"state":"connected","inet":"10.0.6.32","createTimestamp":"2020-12-24T12:29:02.514Z","jre":null,"inet6":null,"name":"LV-ZB17G5-JP","hostname":"JP@LV-ZB17G5-JP","version":"4.24.0","id":338500},"name":"OneNote for Windows 10","capabilities":{"collate":false,"extent":[[2100,2794],[2159,2970]],"bins":["Automatisch selecteren"],"printrate":null,"medias":[],"copies":999,"dpis":["300x300"],"duplex":false,"color":true,"supports_custom_paper_size":false,"papers":{"Letter":[2159,2794],"A4":[2100,2970]},"nup":[]},"id":69957539,"description":"Microsoft Software Printer Driver"}]
I've managed to get the values of the objects, and using recursion to get the objects in the objects. However, I don't understand how to get the arrays. I can ofcourse change these jsonarrays into arrays, but I don't know beforehand how many dimensions an array will have, and also, it makes a difference if the array is a string or a numerical value, which makes it quite hard to parse the values.
I thought I would be able to parse the JSONarray using the ExtractJSONArrayValues(value) procedure, but how do I feed the JSONarray into that procedure? I don't see any options to create a new JSON with that array...