Search code examples
jsonflashactionscriptstringify

JSON members order in ActionScript 3.0


I'm using built-in functionality to create JSON string in Flash app.

Here example of my source code

objStr = JSON.stringify( 
        {
            version:"1.0",
            skin:"white",
            palette:{dataColor:"#0397d6",negativeDataColor:"#d40000",toolbarColor:"#056393"}
        });

I have a problem. Every time I've started my app (not executing createJSON function), I have different member order in JSON string as result. For example:

{"version":"1.0","palette":{"negativeDataColor":"#d40000","dataColor":"#0397d6","toolbarColor":"#056393"},"skin":"white"}

or

{"palette":{"negativeDataColor":"#d40000","toolbarColor":"#056393","dataColor":"#0397d6"},"version":"1.0","skin":"white"}

How can I fix it.


Solution

  • JSON objects are unordered, see JSON.org:

    JSON is built on two structures:

    1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An object is an unordered set of name/value pairs
    2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. An array is an ordered collection of values.

    Order really doesn't matter since you should be retrieving the values by the key rather than iterating over them.