com.fasterxml.jackson.databind.ObjectMapper. .readValue .convertValue

Posted 雪洗中关村

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了com.fasterxml.jackson.databind.ObjectMapper. .readValue .convertValue相关的知识,希望对你有一定的参考价值。

 

    String str="{"student":[{"name":"leilei","age":23},{"name":"leilei02","age":23}]}";
    Student stu = null;
    List<Student> list = null;
    try {
        ObjectMapper objectMapper=new ObjectMapper();
        StudentList studentList=objectMapper.readValue(str, StudentList.class);
        list=studentList.getStudent();
    } catch (Exception e) {
        e.printStackTrace();
    } 
    list.streams.forEach(System::out::println)

 

ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student(); s1.setName("leilei01"); s1.setAge(23);
Student s2=new Student(); s2.setName("leilei02"); s2.setAge(23);
list.add(s1); list.add(s2);
StringWriter str=new StringWriter();
ObjectMapper objectMapper=new ObjectMapper();
try {
    objectMapper.writeValue(str, list);
} catch (Exception e) {
    e.printStackTrace();
}   
System.out.println(str);

 

以上是关于com.fasterxml.jackson.databind.ObjectMapper. .readValue .convertValue的主要内容,如果未能解决你的问题,请参考以下文章