java - put method in the json object adds value to the first of the jsonobject; -


consider following piece of code:

jsonobject json = new jsonobject();   json.put("one", 1);   json.put("two", 2);   json.put("three", 3); 

if print jsonobject prints this

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

but want this.

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

please help. in advance.

the documentation @ http://www.json.org/javadoc/org/json/jsonobject.html says:

a jsonobject unordered collection of name/value pairs.

in other words, properties of object accessed name, not position , default serialized form not guarantee specific order.

strict positioning comes arrays:

jsonarray json = new jsonarray();  json.put("1"); json.put("2"); json.put("3");  json.tostring(); // results in ["1", "2", "3"] 

the easiest workaround solve problem use sortedkeys() method , iterating jsonobject key key, produce json string manually in ever order necessary. implementing custom comparator might also.


Comments

Popular posts from this blog

How to get the arclen between two curve points in Maya? -

C++ Linked List -

git svn - Unignore directories in git-svn (adding directories to a sparse checkout) -