json 映射器 writerWithDefaultPrettyPrinter 用于多个 json 对象,由换行符分隔

json mapper writerWithDefaultPrettyPrinter for multiple json objects separated by new line

我有多行 json 字符串,由换行符分隔。我正在使用 jackson 版本 1.9.13 例如

{"name":"firstPerson","age":25,"gender":"male"}\n
{"name":"secondPerson","age":30,"gender":"male"}\n
{"name":"thirdPerson","age":25,"gender":"male"}\n
...............

根据使用的操作系统,分隔符可能是\n\r或\n。基本上它是一个批记录处理,我们将所有内容保存回数据库。但我们也保存了整个请求。

我的要求是在 prettyprint format.Please 中使用时获取 json 对象的多行 format.Please 找到代码

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

public class Main {


    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, JsonParseException, IOException {
        //mapper.configure(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
        //mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
        String json = "{\"name\":\"firstPerson\",\"age\":25,\"gender\":\"male\"}\n{\"name\":\"secondPerson\",\"age\":30,\"gender\":\"male\"}\n{\"name\":\"thirdPerson\",\"age\":25,\"gender\":\"male\"}";
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectMapper.readValue(json, Object.class)));
        System.out.println(json);
    }
}

输出是:

{
 "name" : "firstPerson",
 "age" : 25,
"gender" : "male"
}

但我想显示为:

{
    "name" : "secondPerson",
    "age" : 30,
    "gender" : "male"
  }
{
  "name" : "thirdPerson",
  "age" : 25,
  "gender" : "male"
}
{
  "name" : "firstPerson",
  "age" : 25,
  "gender" : "male"
}

我知道这不是 json 字符串的标准方式。 json 字符串始终包含在 flower brasis 中。但由于它已经是现有系统,我不想破坏系统来更改代码。它可以简单地使用 java 中的任何字符串处理函数来完成,但我需要使用 jackson 和 jackson 的漂亮打印机。 有办法完成吗?

谢谢, 埃雷什

您的 json 字符串由 3 个 JSON 对象组成,因此您需要一次格式化它们一个,然后将它们连接起来:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
String json = "{\"name\":\"firstPerson\",\"age\":25,\"gender\":\"male\"}\n{\"name\":\"secondPerson\",\"age\":30,\"gender\":\"male\"}\n{\"name\":\"thirdPerson\",\"age\":25,\"gender\":\"male\"}";
String[] jsons = json.split("\n");
String output = "";

for (String str : jsons) {
    output += objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectMapper.readValue(str, Object.class)) + "\n";
}

System.out.println(output);
String person= "{\"name\":\"firstPerson\",\"age\":25,\"gender\":\"male\"}\n{\"name\":\"secondPerson\",\"age\":30,\"gender\":\"male\"}\n{\"name\":\"thirdPerson\",\"age\":25,\"gender\":\"male\"}";

Object json = mapper.readValue(person, Object.class);
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));

就像:
按需

ObjectMapper objectMapper = new ObjectMapper();
Object obj = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectMapper.readValue(stringValue, Object.class));

对于旧版本,将 writerWithDefaultPrettyPrinter() 替换为 defaultPrettyPrinterWriter()

全球

objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Object obj = objectMapper.writeValueAsString(objectMapper.readValue(stringValue, Object.class));