简体   繁体   中英

How do I parse the below JSON with Jackson. What is the most relevant java object to create for using Jackson ObjectMapper?

Assuming I have a JSON response like this:

[{
            "employees" : [{
                    "name" : "Peter",
                    "dob" : "19850101"
                }, {
                    "name" : "Mark",
                    "dob" : "19850202"
                }, {
                    "name" : "Steve",
                    "dob" : "19850303"
                }
            ],
            "projects" : [{
                    "reference" : "P1",
                    "name" : "Project One",
                }, {
                    "reference" : "P2",
                    "name" : "Project Two",
                }, {
                    "reference" : "P3",
                    "name" : "Project Three",
                }
            ],
            "projectMembers" : [{
                    "project" : {
                        "reference" : "P1"
                    },
                    "employees" : [{
                            "name" : "Peter",
                            "dob" : "19850101"
                        }, {
                            "name" : "Steve",
                            "dob" : "19850303"
                        }
                    ]
                }, {
                    "project" : {
                        "reference" : "P2"
                    },
                    "employees" : [{
                            "name" : "Peter",
                            "dob" : "19850101"
                        }, {
                            "name" : "Mark",
                            "dob" : "19850101"
                        }, {
                            "name" : "Steve",
                            "dob" : "19850303"
                        }
                    ]
                },
            ]
        }
    ]

How do I parse this JSON with Jackson. What is the most relevant java object to create for using the Jackson ObjectMapper? Or this should be handled using any custom deserializer?

In my Opinion you will need two basic DTOs. One for the elements refering to the Projects, which will have two fields (reference and name, both as Strings) and one for the Employees, which will also have two fields (name (String) and dob (Long)). Then you will need an extra Class to handle the combination of a Project to multiple employees (this class has two fields: one Project and a List of Employees). To be able to parse the JSON you will then have to create a DTO wrapping your JSON. This Class will have three fields one list of employees, one list of projects and one list of the class assigning employees to projects.
For example something like this:

public class Employee{
   private String name;
   private Long dob;
   //default constructor and getter/setter
}

public class Project{
   private String reference;
   private String name;
   //default constructor and getter/setter
}

public class EmployeesToProject{
  private Project project;
  private List<Employee>;
  //default constructor and getter/setter
}

public class CompleteJSON{
   private List<Employee> employees;
   private List<Project> projects;
   private List<EmployeesToProject> projectMembers;
   //default constructor and getter/setter
}

You can then parse your JSON with the CompleteJSON-Class
You should rename the DTOs to something more readable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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