Android Question Problem with JSON deserialiser

Yayou49

Active Member
Licensed User
Hello everyone,

After a call to a webservice, I have job1.getString whit the following value:

[{"siteId": 7,"siteLibelle": "Belgium"},
{"siteId": 18,"siteLibelle": "Chile"},
{"siteId": 22,"siteLibelle": "Colombia"}]

Using the code provided by Erel:

B4X:
Dim JSON As JSONParser
Dim Map1 As Map
    
JSON.Initialize(Job1.GetString)
Map1 = JSON.NextObject

I've got an error on line:
B4X:
Map1 = JSON.NextObject

Which is:
Error occurred on line: 108 (ParserJSON)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:50)

It seems "[" and "]" are not accepted at the beginning of the string .....
Indeed, if I remove both from the string, it works ....
But they are part of JSON coding ...

Any idea ?
 

NJDude

Expert
Licensed User
Longtime User
This is the correct code:
B4X:
Dim parser As JSONParser 
parser.Initialize(<text>) '<--- Modify this line adding your JSON string.
Dim root As List = parser.NextArray 
For Each colroot As Map In root 
 Dim siteLibelle As String = colroot.Get("siteLibelle") 
 Dim siteId As Int = colroot.Get("siteId") 
Next

Try THIS tool.
 
Upvote 0

Yayou49

Active Member
Licensed User
Thanks NJDude !

It works fine !

But still the question (for my own understanding): why [ and ] are not allowed in my example ?..... that is the question ....
 
Upvote 0
Top