B4J Question JSON order question

ilan

Expert
Licensed User
Longtime User
hi

i am trying to create a JSON string and the order of the output string is different than how I have created it.
obviously, it is my mistake but can someone tell me what I am doing wrong?

B4X:
Sub jsonTry
    Dim js As JSONGenerator
    js.Initialize(CreateMap("fatherName":"ilan","motherName":"rachel","kids":CreateMap("kid1":"noi","kid2":"moria","kid3":"daniel","kid4":"nehorai"),"adress":"haifa","hobbies":CreateMap("parents":CreateMap("hob1":"sleep","hob2":"watching tv"),"kids":CreateMap("hob1":"xbox","hob2":"annoy parents"))))
    Log(js.ToPrettyString(0))
      
    Dim JSON As JSONParser
    Dim Map1 As Map
    JSON.Initialize(js.ToPrettyString(0)) 'Read the text from a file.
    Map1 = JSON.NextObject
    Log("###########")
    recursiveMapReader(Map1)
End Sub

Sub recursiveMapReader(m As Map)
    For Each key As String In m.Keys
        If m.Get(key) Is Map Then
            Log($"${key} : isMap"$)
            recursiveMapReader(m.Get(key))
        Else
            Log($"${key} : ${m.Get(key)}"$)
        End If
    Next
End Sub

output:


Waiting for debugger to connect...
Program started.
{
"fatherName": "ilan",
"hobbies": {
"parents": {
"hob1": "sleep",
"hob2": "watching tv"
},
"kids": {
"hob1": "xbox",
"hob2": "annoy parents"
}
},
"motherName": "rachel",
"adress": "haifa",
"kids": {
"kid1": "noi",
"kid4": "nehorai",
"kid2": "moria",
"kid3": "daniel"
}
}
###########
fatherName : ilan
hobbies : isMap
parents : isMap
hob1 : sleep
hob2 : watching tv
kids : isMap
hob1 : xbox
hob2 : annoy parents
motherName : rachel
adress : haifa
kids : isMap
kid1 : noi
kid4 : nehorai
kid2 : moria
kid3 : daniel
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

ilan

Expert
Licensed User
Longtime User
You have implemented a list using a map. Simply use a list:
B4X:
js.Initialize(CreateMap("fatherName":"ilan","motherName":"rachel","kids": Array("noi", "moria", "daniel", "nehorai"), ...)

Later you will use the regular indices to retrieve items from the list.

it is just an example. i wanted to demonstrate a js object holding another js object that has a key and a value.

example:

B4X:
var pause_menu = {
    pause_button : { someProperty : "prop1", someOther : "prop2" },
    resume_button : { resumeProp : "prop", resumeProp2 : false },
    quit_button : false
};
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
. Why is the order important in this json?

this is a good question that will require me to think about a good answer 😳
😁😁

Actually what i want to do is storing json object in a mysql db and load it back.

I dont think the order is important but in the developing process it would be nice to log the json object and have it clear in front of me or even save it to a file and share it with other api’s.

Thanks, ilan
 
Upvote 0
Top