Page 2 of 2
Re: [probably trivial] Access JSON objects members
Posted: Wed Jun 28, 2017 7:21 pm
by kenmo
vwidmer wrote:how to implement code to handle something like this:
Code: Select all
{"positions":[[123,456,"TEST1"],[234,212,"TEST2"],[982,273,"TEST3",]],"time_on_server":null}
If you try my
JSON_Helper.pbi:
Code: Select all
IncludeFile "JSON_Helper.pbi"
JSON$ = ReplaceString( "{'positions':[[123,456,'TEST1'],[234,212,'TEST2'],[982,273,'TEST3']],'time_on_server':null}" , "'", #DQUOTE$)
ParseJSON(0, JSON$ )
*Pos = JSONArrayFromPath( MainJSONObject(0), "positions")
*Entry = FirstJSONChild(*Pos)
While (*Entry)
Debug JSONIntegerFromPath(*Entry, "[0]")
Debug JSONIntegerFromPath(*Entry, "[1]")
Debug JSONStringFromPath(*Entry, "[2]") + #LF$
*Entry = NextJSONChild(*Pos, *Entry)
Wend
Prints:
123
456
TEST1
234
212
TEST2
982
273
TEST3
Re: [probably trivial] Access JSON objects members
Posted: Wed Jun 28, 2017 8:44 pm
by the.weavster
vwidmer wrote:How do u get something deeper?
like wind > VWINDBAT - SPEED2
or even deeper?
Code: Select all
{"wind":{"SPEED":"14","TEMP":"27","ANGLE":"66","VWINDBAT":{"ANGLE2":"66","SPEED2":"14","TEMP2":"27"}}}
Thanks
Hi vwidmer,
How about this:
Code: Select all
JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
S$ = GetJSONString(GetJSONMember(GetJSONMember(GetJSONMember(nJS,"wind"),"VWINDBAT"),"SPEED2"))
Debug s$
Re: [probably trivial] Access JSON objects members
Posted: Wed Jun 28, 2017 11:48 pm
by vwidmer
@kenmo: Nice can you show me a couple more examples how to use your library with the previous json data. Also if I change the data to this it doesnt pull it all:
Code: Select all
{"positions":[[123.2,456.55,"TEST1"],[-234.2,212,"TEST2"],[982,-273.251,"TEST3",]],"time_on_server":null}
@the.weavster: that is much better then my code as it is much much shorter.
Thank you both very much these are GREAT!
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 12:55 am
by kenmo
vwidmer wrote:Also if I change the data to this it doesnt pull it all:
You have a trailing comma at the end of your array, which is not considered valid JSON syntax. I had to remove that for my example.
You might be able to fix those like this:
Code: Select all
ReplaceString(JSONText$, ",]", "]")
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 1:16 am
by vwidmer
Sorry took it out of a larger file.
Try this :
Code: Select all
{"positions":[[123.2,456.55,"TEST1"],[-234.2,212,"TEST2"],[982,-273.251,"TEST3"]],"time_on_server":null}
What I ment is it will pull only the 123 not 123.2
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 3:23 am
by kenmo
I just updated the includefile, download it again.
I added JSONFloatFromPath() and JSONDoubleFromPath(), you can use those.
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 4:20 am
by vwidmer
@kenmo: you rock! Thanks works very very nice.
Thanks again every one.
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 6:47 am
by the.weavster
vwidmer wrote:@the.weavster: that is much better then my code as it is much much shorter.
No problem.
If you don't want too many levels of nested commands you could do something like this:
Code: Select all
Procedure.i GetJSONChild(nj,pth.s)
nReps = CountString(pth,"|")+1
js = nj
For nLoop = 1 To nReps
js = GetJSONMember(js,StringField(pth,nLoop,"|"))
If Not js : ProcedureReturn -1 : EndIf
Next
ProcedureReturn js
EndProcedure
JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
nJC = GetJSONChild(nJS,"wind|VWINDBAT|SPEED2")
If nJC = -1
Debug "Invalid path"
Else
Debug GetJSONString(nJC)
EndIf
; or for your second example
JSON$ = ~"{\"positions\":[[123.2,456.55,\"TEST1\"],[-234.2,212,\"TEST2\"],[982,-273.251,\"TEST3\"]],\"time_on_server\":null}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
nJC = GetJSONChild(nJS,"positions")
Debug GetJSONDouble(GetJSONElement(GetJSONElement(nJC,0),0))
Re: [probably trivial] Access JSON objects members
Posted: Thu Jun 29, 2017 1:49 pm
by vwidmer
@the.weavster: Wow I like this code as well very nice example. Thank you
Re: [probably trivial] Access JSON objects members
Posted: Mon Jul 03, 2017 10:35 pm
by vwidmer
How to loop the second example to get all?
the.weavster wrote:vwidmer wrote:@the.weavster: that is much better then my code as it is much much shorter.
No problem.
If you don't want too many levels of nested commands you could do something like this:
Code: Select all
Procedure.i GetJSONChild(nj,pth.s)
nReps = CountString(pth,"|")+1
js = nj
For nLoop = 1 To nReps
js = GetJSONMember(js,StringField(pth,nLoop,"|"))
If Not js : ProcedureReturn -1 : EndIf
Next
ProcedureReturn js
EndProcedure
JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
nJC = GetJSONChild(nJS,"wind|VWINDBAT|SPEED2")
If nJC = -1
Debug "Invalid path"
Else
Debug GetJSONString(nJC)
EndIf
; or for your second example
JSON$ = ~"{\"positions\":[[123.2,456.55,\"TEST1\"],[-234.2,212,\"TEST2\"],[982,-273.251,\"TEST3\"]],\"time_on_server\":null}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
nJC = GetJSONChild(nJS,"positions")
Debug GetJSONDouble(GetJSONElement(GetJSONElement(nJC,0),0))
Re: [probably trivial] Access JSON objects members
Posted: Mon Jul 03, 2017 10:40 pm
by vwidmer
How would I grab a child in a child using your mod?
Code: Select all
"Data":[[1,"test 1","test 2",{"min":5,"max":10},false,null]
Thanks
kenmo wrote:I just updated the includefile, download it again.
I added JSONFloatFromPath() and JSONDoubleFromPath(), you can use those.