Hello,
I have a web service written in PHP that returns a JSON string to a B4A app. The JSON string is generated by PHP 5. The string is basically a PHP $_Session variable and contains two objects that are arrays of arrays and several scalar values. I show the string as the first line in the Log output below.
I have listed the code I'm using also. What I would like to be able to do is parse the returned JSON into Maps containing the values, arrays, and objects. I would like the code to be somewhat general-purpose so I don't need to modify it each time the returned string changes a little.
CODE:
OUTPUT (from Android Log):
Is the source code for the JSON Parser available?
Thanks,
Barry.
I have a web service written in PHP that returns a JSON string to a B4A app. The JSON string is generated by PHP 5. The string is basically a PHP $_Session variable and contains two objects that are arrays of arrays and several scalar values. I show the string as the first line in the Log output below.
I have listed the code I'm using also. What I would like to be able to do is parse the returned JSON into Maps containing the values, arrays, and objects. I would like the code to be somewhat general-purpose so I don't need to modify it each time the returned string changes a little.
CODE:
B4X:
Dim js As JSONParser
Dim m As Map
Dim l As List
Dim b As Boolean
Dim i As Int
s = HttpUtils.GetString(PostUrl)
Log(s)
m.Initialize
l.Initialize
js.Initialize(s)
Log("1:")
Try
l = js.NextArray
Log(l)
Catch
Log(LastException.Message)
End Try
js.Initialize(s)
Log("2:")
Try
t = js.NextValue
Log(t)
Catch
Log(LastException.Message)
End Try
js.Initialize(s)
Log("0:")
Try
m = js.NextObject
Log(m)
Catch
Log(LastException.Message)
End Try
Exit
OUTPUT (from Android Log):
B4X:
{"tst1":"test1","groups1":[{"pos":1,"gid":"1","gname":"Family","lastaccess":"100"},{"pos":2,"gid":"2","gname":"Friends","lastaccess":"120"},{"pos":3,"gid":"3","gname":"Coworkers","lastaccess":"130"}],"tst2":"test2","groups2":[{"pos":1,"gid":"1","gname":"Family","lastaccess":"100"},{"pos":2,"gid":"2","gname":"Friends","lastaccess":"120"},{"pos":3,"gid":"3","gname":"Coworkers","lastaccess":"130"}]}
1:
java.lang.RuntimeException: JSON Array expected.
2:
java.lang.RuntimeException: Simple value expected.
0:
(MyMap) {groups1=[{gname=Family, gid=1, lastaccess=100, pos=1}, {gname=Friends, gid=2, lastaccess=120, pos=2}, {gname=Coworkers, gid=3, lastaccess=130, pos=3}], groups2=[{gname=Family, gid=1, lastaccess=100, pos=1}, {gname=Friends, gid=2, lastaccess=120, pos=2}, {gname=Coworkers, gid=3, lastaccess=130, pos=3}], tst1=test1, tst2=test2}
Is the source code for the JSON Parser available?
Thanks,
Barry.