Android Question Parse many different JSON array's

Sub7

Active Member
Licensed User
Longtime User
Hello,
i have to parse several json array, every array is different in size and elements so for a temporary test i created as many subs as many array i have to parse, this solution is muddy many maps, many variables difficult to mantain!

I have over 20 different types of array in my app and making so many subs with so many variables quickly became a mess!

I'd like to make a unique sub that manage all my json array and assign the values to the vars in a smart manner (easy to recognize)

how would you do this ?

Here just two of these subs from the muddy solution:

B4X:
Sub ParseJSON_1(srv_data As String)
    Dim map01 As Map
    map01.Initialize
    jsonParse.Initialize(srv_data)
    map01 = jsonParse.NextObject
    s_data1= map01.get("s_data1")
    s_data2= map01.get("s_data2")
End Sub


Sub parseJSON_2(srv_data As String)
    Dim map03 As Map
    map03.Initialize
    jsonParse.Initialize(srv_data)
    map03 = jsonParse.NextObject
     p_id = map03.get("id")
     p_data_1 = map03.get("p_data1")
     p_data_2 = map03.get("p_data2")
     p_data_3 = map03.get("p_data3")
     p_data_4 = map03.get("p_data4")
     p_data_5 = map03.get("p_data5")
     p_data_6 = map03.get("p_data6")

'There are other 15 subs like this
End Sub

Thank you
 

Sub7

Active Member
Licensed User
Longtime User
A single sub with a select case is a little improovement, still too many vars all around.

Anyone?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to avoid writing such code.

Instead use something like:
B4X:
Sub ParseJson (data As String) As List
Dim res As List
res.Initialize
jsonParse.Initialize(srv_data)
Dim map03 As Map = jsonParse.NextObject
p_id = map03.Get("id")
Dim i As Int = 1
Do While map03.ContainsKey("p_data" & i)
 res.Add(map03.Get("p_data" & i)
 i = i + 1
Loop
return res
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Hello Erel, that's for sure more elegant and functional, thanks.
But in another scenario were the webservice return array's without a constant keyname followed by a number, for example:
{"sid":"xxxxxx", "mac":"xxxxxxx", "vpn_authkey": "xxxx", "session_expire":"xxxxx"}
which would be the best way to don't get lost with names?

Thank you
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Thanks, i am using something like this now so i do not have to assign each field a own variable:

B4X:
Sub process_globals
Dim loginMap, othermap02, othermap04 as map
End sub

Sub parseJSon(srv_data as string, jobType as string)

Select jobType
Case "log"
 
    loginMAP.Initialize
    jsonParse.Initialize(srv_data)
    Dim map03 As Map = jsonParse.NextObject
    Dim values As String
    Dim i As Int = 0
    For Each key As String In map03.Keys
    values = map03.GetValueAt(i)
    loginMAP.Put(key,values)
    i = i +1
    Next

Case "xx" '... and so on

End select

End sub

Later i access the map values: loginMAP.get("keyname")

now i have 15 maps instead of 300 variables, still i cannot clear the maps could this lead to memory problems? these arrays are sometimes very big.

Looks okay to you?

EDIT: Wrong, i can clear map03 when the loop is over.
 
Last edited:
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Thank you so much,
in fact i have no need to copy the map as i can call directly the first map, i were confused by previous muddy solution.
 
Upvote 0
Top