BorisD
BorisD

Reputation: 2261

Titanium Appcelerator - getting JSON in reverse order

I have a JSON as in example: (server side)

myJSON = {
       "details":[
           {"ui":"3"},
           {"vi":"7"},
           {"p" :"c:\"}
       ],
       "flowers":[
           {"bla"},
           {"bla"},
           {"bla"},
       ],
       "examples":[
       ]
};

Now in the Titanium I'm pulling this JSON:

var xhr = Ti.Network.createHTTPClient();
    xhr.onerror = function(e){
                  Ti.API.error('Bad Server =>' + e.error);
          }

    xhr.open('POST', 'http://.....');
    xhr.send();

    xhr.onload = function(){
                 response = JSON.parse(this.responseText);
             }

Now, when I'm trying to alert the "response" value, I see something like this:

 myJSON = {
       "examples":[
       ],
       "flowers":[
           {"bla"},
           {"bla"},
           {"bla"},
       ],
       "details":[
           {"ui":"3"},
           {"vi":"7"},
           {"p" :"c:\"}
       ],
};

Is anybody can give me an idea why is it reversed?

Upvotes: 0

Views: 604

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074385

The order of properties is not guaranteed or significant in JSON (or JavaScript literal notation, or JavaScript objects in general). These objects are equivalent:

{
   "one": 1,
   "two": 2
}

and

{
   "two": 2,
   "one": 1
}

(You also need to escape the \ in {"p" :"c:\"}, e.g.: {"p" :"c:\\"}.)

To maintain order, you have to use an array, as you have for your details and flowers values (note that they have not been reversed).

Also, note that what you've quoted isn't JSON. The giveaway is the myJSON = ... at the beginning of it. What you've quoted is an assignment statement using JavaScript object literal notation (e.g., an object initializer) for the right-hand side.

Upvotes: 3

Related Questions