-2
public class Town {
private Person p;
private String hello;
private long number;
}
public class Person {
private String firstName;
private double legs;
private String lastName;
}
我想寫使用下面的代碼如何閱讀對象的JSON文件到列表中的Java與傑克遜
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
writer.writeValue(out, townobj);
將會產生的Json喜歡這個城市類JSON。
{
"p" : {
"firstName" : "John",
"amount" : 6860.0,
"lastName" : "Smith"
},
"hello" : "qwiejiowcqnio",
"number" : 1380.0
}
{
"p" : {
"firstName" : "Sam",
"amount" : 623460.0,
"lastName" : "Smith"
},
"hello" : "qwiej2342io",
"number" : 1330.0
}
如何從jackson中讀取嵌套對象轉換爲Java的輸出?
所以你知道如何使用'mapper.writer()'。當看[ObjectMapper'](https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html)的javadoc時,它不是很明顯,相反的操作會使用'mapper.reader()'? – Andreas