CoderDecoder
CoderDecoder

Reputation: 445

JSON string not in order

I want the JSON string to be in same order how I am putting it.This is my query.

object.put("name", name);
object.put("email", email);
object.put("query", query);

But in the resultant string its showing as

{"email""[email protected]","query":"k","name":"a"}

Upvotes: 1

Views: 95

Answers (1)

Amadan
Amadan

Reputation: 198436

The order of keys in a JS object is not guaranteed. If you need a particular order, consider having a separate array of keys to preserve the ordering.

{
  "order":["name", "email", "query"],
  "data":{
    "email":"[email protected]",
    "query":"k",
    "name":"a"
  }
}

From JSON specification http://www.ietf.org/rfc/rfc4627.txt:

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

(emphasis mine)

Upvotes: 3

Related Questions