Android Question JSONGenerator.To(Pretty)String sequence different from map?

KMatle

Expert
Licensed User
Longtime User
I mentioned that the sequence of the generated ToString or ToPrettyString is different from the key/values in the map used.

Examples

Map:

key1, value1
key2, value2
key3, value3
...
...
keyn, valuen

After JSONGenerator.Initialize(MyMap) & "ToString" or "ToPrettyString":

key1, value1, key3, value3, key7, value7, key2, value2 and so on...

This is not a problem but am I blind (didn't see the paramater causing this) or what is the reason for the de-sequencing? I would have expected a 1:1 structure (1,2,3,4 ... and not "randomly" 1,3,7,2)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Generally speaking maps (or hashtables) data structures do not have any specific order. B4A Map does have a consistent order. However the native JSON parser doesn't respect it.

Note that the main reason for implementing a map with a consistent order was to allow iterating over the items with Map.GetKeyAt / GetValueAt. This was before the For Each loop was introduced.
 
Upvote 0
Top