简体   繁体   English

使用Jackson将JSON反序列化为Java

[英]Deserializing JSON to Java using Jackson

I am working with JSON for the very first time and I am trying to deserialise a JSON file into Java objects. 我第一次使用JSON,并且尝试将JSON文件反序列化为Java对象。

InputJSON InputJSON

{

    "student_id" : "123",
     "courses":[
        {
         "course_id":"789",
         "professor":"abc"
        }
      ]
}
{
    "student_id":"234",
     courses:[
       {
         "course_id":"789",
         "professor":"pqr"
       },
       {
         "course_id":"789",
         "professor":"xyz"
       }
     ]
}
{
   "student_id" : "345",
     "courses":[
        {
         "course_id":"567",
         "professor":"lmn"
        }
      ]
}

Student.class Student.class

class Student {

   @JsonProperty("student_id")
   private String studentId;
   @JsonProperty("courses")
   private List<Courses> courses;

   //getters and setters

}

Courses.class Courses.class

public class Courses {

    @JsonProperty("course_id")
    private String courseId;
    @JsonProperty("professor")
    private String professor;

    //getters and setters
}

My JsonHelper.class creates an object of Object Mapper class and uses readValue(new File("Input.json"),Student.class) to to map the json fields to Java objects. 我的JsonHelper.class创建一个Object Mapper类的对象,并使用readValue(new File(“ Input.json”),Student.class)将json字段映射到Java对象。

What I want to do is create a map with studentId as a key and the list of courses as the value corresponding to each studentId. 我想做的是创建一个带有StudentId作为键的地图,并将课程列表作为与每个StudentId对应的值。

I am not exactly getting how to achieve it since my list is just able to get 1 element and the list size is thus 1. And issue with creating the map too. 由于我的列表仅能获得1个元素,并且列表大小因此为1,因此我并没有确切地实现它。而且,创建地图也存在问题。 Any help will be appreciated. 任何帮助将不胜感激。

This java class seems wrong. 这个java类似乎是错误的。 because your variable name is not the same with your JSON object. 因为您的变量名与JSON对象不同。

public class Courses {
    @JsonProperty("course_id")
    private String courseId;
    @JsonProperty("professor")
    private String professor;
    //getters and setters
}

it would be like this, 就像这样

class Student {
   @JsonProperty("student_id")
   private String student_id;
   @JsonProperty("courses")
   private List<Courses> courses;
   //getters and setters

    }
    public class Courses {
            @JsonProperty("course_id")
            private String course_id;
            @JsonProperty("professor")
            private String professor;
            //getters and setters
        }

Here are some ways to do mapping with ObjectMapper: 以下是一些使用ObjectMapper进行映射的方法:

ObjectMapper objectMapper = new ObjectMapper();

For parsing a single Student object: 对于解析单个Student对象:

Student student = objectMapper.readValue(inputJSONString, Student.class);

For parsing a list of Student object which is what you want: 要解析所需的Student对象列表:

List<Student> students = objectMapper.readValue(inputJSONString, objectMapper.getTypeFactory().constructCollectionType(List.class, Student.class));

For parsing a maps: 对于解析地图:

Map<String, Courses> maps = objectMapper.readValue(inputJSONString, objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Courses.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM