ambarket / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

JSONObject.put() stores things in incorrect order #72

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. add these value in sequence into JSONObject: 
(1,1),(2,2),(3,3),(4,4),(5,5),(6,6)
2. then retrieve it from the object:  
{"3":"3","2":"2","1":"1","6":"6","5":"5","4":"4"}
3. watch how it behave carefully step-by-step:

DEBUG: I put this: 1,1
DEBUG: I got this array: {"1":"1"}
DEBUG: I put this: 2,2
DEBUG: I got this array: {"2":"2","1":"1"}
DEBUG: I put this: 3,3
DEBUG: I got this array: {"3":"3","2":"2","1":"1"}
DEBUG: I put this: 4,4
DEBUG: I got this array: {"3":"3","2":"2","1":"1","4":"4"}
DEBUG: I put this: 5,5
DEBUG: I got this array: {"3":"3","2":"2","1":"1","5":"5","4":"4"}
DEBUG: I put this: 6,6
DEBUG: I got this array: {"3":"3","2":"2","1":"1","6":"6","5":"5","4":"4"}
DEBUG: result json:{"3":"3","2":"2","1":"1","6":"6","5":"5","4":"4"}

What is the expected output? What do you see instead?
Expected: {"1":"1","2":"2","3":"3","4":"4","5":"5","6":"6"}
Result: {"3":"3","2":"2","1":"1","6":"6","5":"5","4":"4"}

What version of the product are you using? On what operating system?
I'm using the latest featured json-simple-1.1.1.jar 

Please provide any additional information below.

//Code that replicate the problem simply
import org.json.simple.JSONObject;

public class JSONTest {
    /**
     * @param args
     */
    public static void main(String[] args) {

        JSONObject obj = new JSONObject();
        obj.put("1","1");
        System.out.println("Result Json: " + obj.toString());
        obj.put("2","2");
        System.out.println("Result Json: " + obj.toString());
        obj.put("3","3");
        System.out.println("Result Json: " + obj.toString());
        obj.put("4","4");
        System.out.println("Result Json: " + obj.toString());
        obj.put("5","5");
        System.out.println("Result Json: " + obj.toString());
        obj.put("6","6");
        System.out.println("Result Json: " + obj.toString());
        obj.put("7","7");
        System.out.println("Result Json: " + obj.toString());
        obj.put("8","8");
        System.out.println("Result Json: " + obj.toString());
        obj.put("9","9");
        System.out.println("Result Json: " + obj.toString());
    }

}

Original issue reported on code.google.com by waltert...@gmail.com on 19 May 2012 at 11:24

GoogleCodeExporter commented 9 years ago
JSON object is NOT guaranteed to be ordered according to the spec, but you can 
use java.util.LinkedHashMap if you want to preserve the order on output. See 
http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_
a_JSON_object

Original comment by fangyid...@gmail.com on 21 May 2012 at 9:07

GoogleCodeExporter commented 9 years ago
Thanks. 

I also found quite an interesting workaround in StackWorkflow: 
http://stackoverflow.com/a/7981662

:-)

Original comment by waltert...@gmail.com on 21 May 2012 at 3:17