JSON漂亮的打印而不改变顺序

时间:2021-07-21 21:44:42

I use json-simple and want to have pretty-print for debugging purposes.

我使用json-simple并希望有漂亮的打印用于调试目的。

Here is a very relavant SO question: Pretty-Print JSON in Java

这是一个非常相关的SO问题:Java中的Pretty-Print JSON

However the answer in the given thread, not only fixes the indentation but also changes the order of the items to [a ... z] using the string order of the keys.

然而,给定线程中的答案不仅修复了缩进,而且还使用键的字符串顺序将项的顺序更改为[a ... z]。

Is there any way to fix the indentation without changing the order of the items in my JSONObject?

有没有办法修改缩进而不改变我的JSONObject中项目的顺序?

Example:

JSONObject myJSon = new JSONObject();
myJSon.put("zzz", 1);
myJSon.put("aaa", 1);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println( gson.toJson(myJSon) );

Output:

{
  "aaa": 1,
  "zzz": 1
}

Desired output:

{
  "zzz": 1,
  "aaa": 1
}

Edit: I'm using: org.json.simple.JSONObject

编辑:我正在使用:org.json.simple.JSONObject

2 个解决方案

#1


7  

org.json.simple.JSONObject class extends java.util.HashMap and this is the reason why you see this order of the properties on output. setPrettyPrinting() method doesn't change the order. You can remove it from source code and nothing change. If you want to keep the order you can use java.util.LinkedHashMap instead of org.json.simple.JSONObject.

org.json.simple.JSONObject类扩展了java.util.HashMap,这就是为什么你在输出中看到这个属性的顺序的原因。 setPrettyPrinting()方法不会更改顺序。您可以从源代码中删除它,没有任何更改。如果要保留顺序,可以使用java.util.LinkedHashMap而不是org.json.simple.JSONObject。

Simple example:

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonProgram {

    public static void main(String[] args) throws Exception {
        Map<String, Integer> myJSon = new LinkedHashMap<String, Integer>();
        myJSon.put("zzz", 1);
        myJSon.put("aaa", 1);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(myJSon));
    }
}

#2


0  

No. JSONObject is a HashMap, there is no order maintained of the entries you put into it.

不.JSONObject是一个HashMap,没有维护您放入其中的条目的顺序。

#1


7  

org.json.simple.JSONObject class extends java.util.HashMap and this is the reason why you see this order of the properties on output. setPrettyPrinting() method doesn't change the order. You can remove it from source code and nothing change. If you want to keep the order you can use java.util.LinkedHashMap instead of org.json.simple.JSONObject.

org.json.simple.JSONObject类扩展了java.util.HashMap,这就是为什么你在输出中看到这个属性的顺序的原因。 setPrettyPrinting()方法不会更改顺序。您可以从源代码中删除它,没有任何更改。如果要保留顺序,可以使用java.util.LinkedHashMap而不是org.json.simple.JSONObject。

Simple example:

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonProgram {

    public static void main(String[] args) throws Exception {
        Map<String, Integer> myJSon = new LinkedHashMap<String, Integer>();
        myJSon.put("zzz", 1);
        myJSon.put("aaa", 1);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(myJSon));
    }
}

#2


0  

No. JSONObject is a HashMap, there is no order maintained of the entries you put into it.

不.JSONObject是一个HashMap,没有维护您放入其中的条目的顺序。