json mapper writerWithDefaultPrettyPrinter用于由新行分隔的多个json对象

时间:2015-04-17 12:34:26

标签: java json jackson

我有多行json字符串由换行符分隔。我使用的是杰克逊版本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格式时获取多行json对象。请找到代码

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字符串总是包含在花辫子中。但由于它已经是现有的系统,我不想破坏系统来改变代码。它可以简单地使用java中的任何字符串处理函数来完成,但我需要使用jackson和jackson的漂亮打印机。 有没有办法完成这项工作?

谢谢, Eresh

3 个答案:

答案 0 :(得分:2)

你的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);

答案 1 :(得分:1)

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));

答案 2 :(得分:0)

就像:
按需

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));