1、将Java对象转换为JSON
ObjectMapper mapper = new ObjectMapper();
Staff obj = new Staff();
mapper.writeValue(new File("d:\\file.json"), obj);
String jsonInString = mapper.writeValueAsString(obj);
writeValue还支持的方法如下图所示:
2、Jave实体类之间转换
ObjectMapper mapper = new ObjectMapper();
String answer= "{'name' : 'mkyong'}";
CyAnswerTransport cyAnswerTransport = mapper.readValue(answer, CyAnswerTransport.class);
readValue还支持的转换方法如下图所示:
3、字符串对象转List对象
ObjectMapper mapper = new ObjectMapper();
String content = "";
List<CyAnswerDetail> answerDetailList = mapper.readValue(content,new TypeReference<List<CyAnswerDetail>>(){});