日期:2014-05-20  浏览次数:20696 次

json与map装换的问题
Java code

    Map map = new HashMap();
        map.put("name", "json");
        map.put("bool", Boolean.TRUE);
        map.put("int", new Integer(1));
        map.put("arr", new String[] { "a", "b" });
        JSONObject json = JSONObject.fromObject(map);
        System.out.println(json);



输出为:
{"arr":["a","b"],"int":1,"bool":true,"name":"json"}

暂时可以理解成,取的时候是从后往前取

在看例子:
Java code

Map map = new HashMap();
        map.put("name", "json");
        map.put("arr", new String[] { "a", "b" });
        map.put("int", new Integer(1));
        map.put("bool", Boolean.TRUE);
        JSONObject json = JSONObject.fromObject(map);
        System.out.println(json);


输出为:
{"arr":["a","b"],"int":1,"bool":true,"name":"json"}

可以看出:数组默认在前,int其次,然后就是boolean和String

现在想请问下如何按照我想输出的类型输出
比如说,先是String,然后是数组和boolean

------解决方案--------------------
HashMap是不维持顺序的,它内部会自动排序。
可以使用LinkedHashMap,这个实现为维持顺序的