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:


 

ilan

Expert
Licensed User
Longtime User

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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…