Android Question JSON Parsing

microbox

Active Member
Licensed User
Longtime User
I am trying to query from a particular dynamic table and then need to query back its field names and the values.
This is the response from the server: Response from server: [{"id":"1","field1":"25","field2":"25","field3":"25","field4":"25"}];
Here is the code I'm using
B4X:
Dim v1,v2 As String
Dim ListOfItems As List = parser.NextArray
Dim fieldlist As Map ' reg_id
    For i = 0 To ListOfItems.Size - 1
        fieldlist = ListOfItems.Get(i)
        v1 = fieldlist.GetKeyAt(i)
        v2 = fieldlist.GetValueAt(i)
        Log(v1 & " " & v2)
    Next
but the code above output this
id 1
It seems ListOfItems contains only 1 size item.

Thanks for the time and keep safe.
microbox
 

DonManfred

Expert
Licensed User
Longtime User
Where is the json-file? Without knowing the json Structure no one can help here.
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
I entered this
B4X:
[{"id":"1","field1":"25","field2":"25","field3":"25","field4":"25"}];
And the results gives me.
B4X:
0
field1: 25
id: 1
field3: 25
field2: 25
field4: 25
And the codes
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim field1 As String = colroot.Get("field1")
 Dim id As String = colroot.Get("id")
 Dim field3 As String = colroot.Get("field3")
 Dim field2 As String = colroot.Get("field2")
 Dim field4 As String = colroot.Get("field4")
Next
Is there a way not to hard code them(Dim field1 As String = colroot.Get("field1")) but like
v1 = fieldlist.GetKeyAt(i)
log(v1)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Thanks for the link I appreciate it very much.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
It seems ListOfItems contains only 1 size item.
ListOfItems only contains 1 item: the json object with the k/v's
your counter should refer to fieldlist.size (if you must use getkeyat(), which we're not supposed to do. using keys is recommended). regardless, it's the size of the map that you want, not the size of the list.

to be clear, you're going to use 2 counters: 1 to loop through the list of items, and the second counter to step through the k/v's in each json object within the list. yes, i realize that the list only has 1 item, but you set up the loop as if you didn't know that

B4X:
Dim v1,v2 As String
dim i, j as int

Dim ListOfItems As List = parser.NextArray
for i = 0 to listofitems.size - 1
      Dim fieldlist As Map = listofitems.get(i)  ' reg_id
      For j = 0 To fieldlist.Size - 1
        v1 = fieldlist.GetKeyAt(j)
        v2 = fieldlist.GetValueAt(j)
        Log(v1 & " " & v2)
      Next
next

'note: recommended to use For Each field As string In fieldlist.Keys instead of getkeyat/getvalueat
'note: maybe change "fieldlist" to "fieldmap" to avoid thinking of fieldlist as a list
'note: you could also step through your listofitems with:  for each item as map in listofitems
 
Last edited:
Upvote 0
Top