1

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"}

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

It wont affect the php script?
If you change the format of the protocol, you definitely should also change the script on the other end of the communication channel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.