[英]How to have each record of JSON on a separate line?
When I use JSONArray and JSONObject to generate a JSON, whole JSON will be generated in one line. 当我使用JSONArray和JSONObject生成JSON时,整个JSON将在一行中生成。 How can I have each record on a separate line?
如何将每条记录放在单独的行上?
It generates like this: 它生成如下:
[{"key1":"value1","key2":"value2"}]
I need it to be like following: 我需要它如下所示:
[{
"key1":"value1",
"key2":"value2"
}]
You can use Pretty Print JSON
Output (Jackson). 您可以使用
Pretty Print JSON
Output(Jackson)。
Bellow are some examples 波纹管是一些例子
Convert Object and print its output in JSON format. 转换对象并以JSON格式输出其输出。
User user = new User(); //...set user data ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
Pretty Print JSON String 漂亮的打印JSON字符串
String test = "{\\"age\\":29,\\"messages\\":[\\"msg 1\\",\\"msg 2\\",\\"msg 3\\"],\\"name\\":\\"myname\\"}"; Object json = mapper.readValue(test, Object.class); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
Reference : http://www.mkyong.com/java/how-to-enable-pretty-print-json-output-jackson/ 参考: http : //www.mkyong.com/java/how-to-enable-pretty-print-json-output-jackson/
You may use of the google-gson
library for beautifying your JSON string. 您可以使用
google-gson
库来美化JSON字符串。 You can download the library from here 您可以从这里下载库
Sample code : 样例代码:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);
OR 要么
you can use org.json
你可以使用
org.json
Sample code : 样例代码:
JSONTokener tokener = new JSONTokener(uglyJsonString); //tokenize the ugly JSON string
JSONObject finalResult = new JSONObject(tokener); // convert it to JSON object
System.out.println(finalResult.toString(4)); // To string method prints it with specified indentation.
Refer answer from this post : Pretty-Print JSON in Java 请参阅这篇文章的答案: Java中的Pretty-Print JSON
The JSON.stringify method supported by many modern browsers (including IE8) can output a beautified JSON string: 许多现代浏览器(包括IE8)支持的JSON.stringify方法可以输出美化的JSON字符串:
JSON.stringify(jsObj, null, "\t"); // stringify with tabs inserted at each level
JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level
and please refer this : https://stackoverflow.com/a/2614874/3164682 并请参考: https : //stackoverflow.com/a/2614874/3164682
you can also beautify your string online here.. http://codebeautify.org/jsonviewer 您还可以在此处在线美化您的字符串。.http://codebeautify.org/jsonviewer
为了获得易于阅读的json文件,您可以使用以下命令将ObjectMapper配置为缩进:
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.