我正在尝试使用Jackson创建一个JSON文档。层次结构如下:
事件:
class Event {
private String name = "";
private Set<Integer> admin = new HashSet<>();
private List<House> houseList = new ArrayList<>();
}
行:
class House {
private List<OG> OGList = new ArrayList<>();
private int score = 0;
private String name = "";
}
组:
class OG {
private int score = 0;
private int id = 0;
}
每个事件可能包含一定数量的房屋,而房屋又包含一定数量的房屋。每个房屋和团体都有一个分数修改器。
目前,这是我使用漂亮的打印方法打印JSON文档的方式:
ObjectMapper mapper = new ObjectMapper();
File f = new File("./db/" + dir);
if (f.exists() && !f.isDirectory()) {
return "This event name is taken. Please try again.";
}
try {
mapper.writeValue(f, event);
// Convert object to JSON string and pretty print
String jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(event);
System.out.println(jsonInString);
}
}
结果输出非常难看:
{
"name" : "test",
"admin" : [ 423766405 ],
"houseList" : [ {
"score" : 0,
"name" : "first",
"oglist" : [ {
"score" : 0,
"id" : 0
}, {
"score" : 0,
"id" : 1
}, {
"score" : 0,
"id" : 2
} ]
..
}
是否有更好的格式化输出的方法,例如:
name:
test
admin:
a
b
c
houses:
name:
first
group:
1
..