[英]Json Object Array Write in Java line by line
I have a Array with some value when i store that array i got result like this 当我存储该数组时,我有一个带有某些值的数组,我得到这样的结果
[{"id":56678,"Name":"Rehman Agarwal"},{"id":66849,"Name":"Rasul Guha"}]
means in a single line. 表示一行。
but I just want to get output like 但我只想得到像
[{"id":56678,"Name":"Rehman Agarwal"},
{"id":66849,"Name":"Rasul Guha"}]
new line after JSON object .. JSON对象之后的新行..
How to do it ? 怎么做 ?
EDIT : i use JSONObject && JSONArray
for create json Object and array respectively 编辑: 我使用
JSONObject && JSONArray
分别创建json对象和数组
Using gson library you can pretty print your json strings like below - 使用gson库,您可以像下面这样漂亮地打印json字符串-
public static String toPrettyFormat(String jsonString) {
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
return prettyJson;
}
And call it like below - 然后像下面这样称呼它-
public void testPrettyPrint() {
String compactJson = "{\"playerID\":1234,\"name\":\"Test\",\"itemList\":[{\"itemID\":1,\"name\":\"Axe\",\"atk\":12,\"def\":0},{\"itemID\":2,\"name\":\"Sword\",\"atk\":5,\"def\":5},{\"itemID\":3,\"name\":\"Shield\",\"atk\":0,\"def\":10}]}";
String prettyJson = toPrettyFormat(compactJson);
System.out.println("Compact:\n" + compactJson);
System.out.println("Pretty:\n" + prettyJson);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.