Json Order Mixed Up

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.

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..

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.

Why Json Object order mixed up in android KITKAT and below versions?

a JSONObject doesn't guarantee any order for its keys, it may sometimes be in the order of insertion, and sometimes not.

If you need to keep the order of insertion, use a JSONArray instead.

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.

Converting json string to json object as it is in Java

org.json uses HashMap to store the parsed data. So you cannot guarantee the order.

Ideally ordering of the elements should not matter. But if you must need that and can change your json library then use fasterxml-jackson. It uses LinkedHashMap and preserves the order.

Also note that org.json is very basic + lightweight(66KB) library that does the job. Jackson is at least 1.7MB+.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class JsonTest {

String responseData = "{ \"contents\" : [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}";

@Test
void orgJson() {
JSONObject jsonObject = new JSONObject(responseData);
System.out.println(jsonObject.toString()); //no ordering
}

@Test
void jackson() throws JsonProcessingException {
JsonNode jsonObject = new ObjectMapper().readTree(responseData);
System.out.println(jsonObject.toString()); //keeps the ordering
}
}


Related Topics



Leave a reply



Submit