JSONobject:Why JSONobject Changing the Order of Attributes

Ordered JSONObject

As JSON objects do not inherently have an order, you should use an array within your JSON object to ensure order. As an example (based on your code):

 jsonObj = 
{ items:
[ { name: "Stack", time: "..." },
{ name: "Overflow", time: "..." },
{ name: "Rocks", time: "..." },
... ] };

This structure will ensure that your objects are inserted in the proper sequence.

Based on the JSON you have above, you could place the objects into an array and then sort the array.

 var myArray = [];
var resultArray;

for (var j in jsonObj) {
myArray.push(j);
}

myArray = $.sort(myArray, function(a, b) { return parseInt(a) > parseInt(b); });

for (var i = 0; i < myArray.length; i++) {
resultArray.push(jsonObj[myArray[i]]);
}

//resultArray is now the elements in your jsonObj, properly sorted;

But maybe that's more complicated than you are looking for..

How to maintain the order of a JSONObject

You can't.

That is why we call it an unordered collection of name/value pairs.

Why you would need to do this, I'm not sure. But if you want ordering, you'll have to use a json array.

Is it possible to change order of attributes when toJson()?

Field/member/attributes in a JSON collection do not have an order, and as far as this JSON data structure is concerned, the "order" doesn't matter.

The only reason I can imagine you are concerned with the order is for printing/presentation purposes. In that case I suggest you manually construct the JSON string yourself.

Does the sequence of the values matter in a JSON object?

My question is that does the order of the JSON object really matters on the server side?

It should not matter. According to various JSON specifications, the order of the attributes is not significant. For example:

"An object is an unordered set of name/value pairs." (Source json.org)

"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." (Source RFC 7159)

Unfortunately, there are nitwits out there1 who ignore that aspect of the specs, and place some significance on the order of the attributes. (The mistake is usually made when there is a disconnect between the people specifying the APIs and those implementing them, and the people doing the specification work don't really understand JSON.)

Fortunately, the chances are that whoever designed / implemented the server didn't make that mistake. Most Java JSON parsers I've come across don't preserve the attribute order when parsing ... by default2. It would be hard to accidentally implement a server where the order of the JSON attributes being parsed was significant.

If yes, how can i change the order?

With difficulty, I fear:

  • You could generate the JSON by hand.
  • There is at least one JSON for java implementation3 that allows you to supply the Map object that holds a JSON object's attributes. If you use a LinkedHashMap or TreeMap, it should retain the insertion order or the lexical order of the attribute keys.

1 - For example, the nitwits that this poor developer was working for ... https://stackoverflow.com/a/4515863/139985

2 - RFC 7159 also says this: "JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.". By my reading, this recommends that JSON libraries should hide any order of the pairs from application code.

3 - JSON-simple : https://code.google.com/p/json-simple/. There could be others too.

JSON: Order of items

From http://json.org (my emphasis):

An object is an unordered set of name/value pairs.

So this

{ "foo": 1, "bar": 2 }

and this

{ "bar": 2, "foo": 1 }

is the same object. Any library that is looking for an order is looking for something that does and can not exist. Code can output a JSON object in any order it pleases.

Keep the order of the JSON keys during JSON conversion to CSV

Solved.

I used the JSON.simple library from here https://code.google.com/p/json-simple/ to read the JSON string to keep the order of keys and use JavaCSV library from here http://sourceforge.net/projects/javacsv/ to convert to CSV format.

Is the order of elements in a JSON list preserved?

Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format
(emphasis mine):

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.

An array is an ordered sequence of zero or more values.

The terms "object" and "array" come from the conventions of
JavaScript.

Some implementations do also preserve the order of JSON objects as well, but this is not guaranteed.



Related Topics



Leave a reply



Submit