Android Question For Each JSON Item in List?

Homerclese

Member
Licensed User
Longtime User
I have some JSON that I need to parse, which looks like this:

B4X:
[ 
   { 
      "Id":1,
      "Name":"ABC"
   },
   { 
      "Id":2,
      "Name":"XYZ"
   }
]

Because the top level isn't an object, I am parsing it and converting it to a List like this:

B4X:
Dim JSON As JSONParser
JSON.Initialize(Job.GetString)
               
Dim JSONList As List = JSON.NextArray

That works. But then I want to convert each element in the list into a Map. I know I can use a For/Next loop to loop through the list but is there a more elegant want to do this using a For/Each loop, e.g.:

B4X:
For Each JSONMap As ??? In JSONList

Next

If so, what is the type of entity?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim Id As Int = colroot.Get("Id")
Dim Name As String = colroot.Get("Name")
Next

See http://basic4ppc.com:51042/json/index.html
 
Upvote 0
Top