首页 > 其他 > 详细

Jackson简单数据绑定

时间:2019-05-09 18:52:12      阅读:118      评论:0      收藏:0      [点我收藏+]
public class JacksonTester3 {
    public static void main(String[] args) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> studentDataMap = new HashMap<String, Object>();
            Student student = new Student();
            student.setAge(18);
            student.setName("出??");
            int[] marks = {1, 2, 3};

            studentDataMap.put("student", student);
            studentDataMap.put("name", "Mahesh Kumar");
            studentDataMap.put("verified", Boolean.FALSE);
            studentDataMap.put("marks", marks);

            mapper.writeValue(new File("student2.json"), studentDataMap);

            studentDataMap = mapper.readValue(new File("student2.json"), Map.class);

            System.out.println(studentDataMap.get("student"));
            System.out.println(studentDataMap.get("name"));
            System.out.println(studentDataMap.get("verified"));
            System.out.println(studentDataMap.get("marks"));
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Lombok
class Student{
  private String name;
  private Integer age;
}

 

Jackson简单数据绑定

原文:https://www.cnblogs.com/mlyun/p/10840272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!