iOS Question Error in Parsing JSON (list in a map)

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi everyone,

consider the following JSON:

B4X:
{"root":[{"VersionID":18,"SelectedVersion":1},{"VersionID":20,"SelectedVersion":2}]}

This JSON cannot be parsed in B4i v5.50.
The first step is concluded successfully but if you try to get the list you 're toasted for good (check the attached project). Any ideas anyone?

Thanks in advance
 

Attachments

  • JSONProblemParseList.zip
    1.3 KB · Views: 148

Semen Matusovskiy

Well-Known Member
Licensed User
Looks like correct JSON and works without problems
B4X:
    Dim sJSON As String = $"{"root":[{"VersionID":18,"SelectedVersion":1},{"VersionID":20,"SelectedVersion":2}]}"$
   
    Dim parser As JSONParser
    parser.Initialize (sJSON)   
    Dim map As Map = parser.NextObject
    Dim list As List = map.Get("root")
    For Each colroot As Map In list
        Log ("SelectedVersion = " & colroot.Get ("SelectedVersion"))
        Log ("VersionID = " & colroot.Get ("VersionID"))
    Next
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I do not understand. The code by Semen works with me too. My code in the attached project does not work in b4i but works perfect in b4a as it is copied from the same project in b4a which works.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Nothing strange. Your assign List to String in
B4X:
sJSON = m.Get("root")
Very doubt that there is a standard for similar conversion. Anyway, no reason to expect the same results in B4I and in B4A.

BTW, your code is very redundant. For example, your initialize map/list and then get it from the parser.
And to initialize parser one time is enough.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I don't really consider the code redundant. That is called defensive programming. My moto is "throw and handle exceptions only when you need them".
 
Upvote 0
Top