我想将一些json保存到文件
这是我的序列码:
private String serializeToJson(T item) {
String json;
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
json = ow.writeValueAsString(item);
} catch (IOException e) {
e.printStackTrace();
json = "";
}
return json;
}
然而我的json保存到未格式化的文件:
e.g。部分原因是:
{"路径" {" segmentId":69798169,"节点ID":18477384," X": - 71.12074&# 34; Y":42.33235}"街道":1," altStreets":空,"距离":59,"长度&#34 ;:178," crossTime":49," crossTimeWithoutRealTime":49,"瓷砖":[0,1],"的clientId&#34 ;: [166177],"指令":
答案 0 :(得分:0)
尝试杰克逊漂亮的打印映射器?假定对象已实现toString方法
ObjectMapper mapper = new ObjectMapper();
try {
System.out.println("Default output:"+mapper.writeValueAsString(object));
System.out.println("Pretty printing:\n"+mapper.defaultPrettyPrintingWriter().writeValueAsString(object));