简体   繁体   English

如何将每条JSON记录放在单独的行上?

[英]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 波纹管是一些例子

  1. 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)); 
  2. 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.

相关问题 如何将每个值添加到单独的行? - How to add each value to a separate line? 为每个值获取单独的记录 - Get separate record for each value 如何为每个sql记录创建单独的输出文件? - How do I create separate output file for each sql record? java - 如何将数组更改为每个名称在java中单独一行的字符串? - How to change an arrays into a String with each name on a separate line in java? 如何为我在activemq中拥有的每个消费者创建单独的队列? - How to create separate queue for each consumer I have in activemq? 如何反序列化深度嵌套的 JSON 而无需为每个级别单独的类 - How to deserialize deeply nested JSON without separate classes for each level 如何在Java中一行一行地读取文本文件,并分别分隔每一行的内容? - How do you read a text file, line by line, and separate contents of each line in Java? 带JSON编解码器的Logstash TCP输入将每行视为一个单独的事件 - Logstash TCP input w/ JSON codec treats each line as a separate event 如何将JSON文件加载到每个记录具有唯一名称的Java中? - How to load a JSON file into java that has unique name for each record? Java将每一行读入单独的ArrayList(字符串) - Java Read Each Line Into Separate ArrayList (String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM