如何用jackson

时间:2017-05-17 11:25:03

标签: java json jackson

如何从JSON文件中读取Farm对象?

public class Farm(){
        private Animal cat;
        private String name;
        private int age;
}
public class Animal(){
        private int legs;
} 

我使用以下代码将Farm对象写入文件。

ObjectMapper mapper = new ObjectMapper();                   
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
mapper.writeValue(out, farmobj);

1 个答案:

答案 0 :(得分:1)

Farm farm = mapper.readValue(new File("c:\\farm.json"), Farm.class);

您正在寻找的是什么。