POJO class 带有嵌套 JSON 的 POST 请求主体的构造函数

POJO class with Constructor for the POST request body which has nested JSON

我想为嵌套了 JSON 的 POST 请求主体创建一个带有构造函数的 POJO class,但我不确定如何调用 JSONArray在里面?

PS: 我不想用setter方式设置数据,我想用Constructor来设置数据。

这里是 JSON:

{
"FirstName": "test",
"LastName": "account",
"PASSWORD": "Password123*",
"Email": [
    {
        "TYPE": "Primary",
        "VALUE": "arpitay6@mail7.io"
    }


]}

我创建的 POJO -


import java.util.List;

public class PostAccountCreateAPI {

    private List <Email> email;
    private String password;
    private String firstname;
    private String lastname;
    
    public PostAccountCreateAPI(List<Email> email, String password, String firstname, String lastname) {
        
        this.email = email;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Email> getEmail() {
        return email;
    }

    public void setEmail(List<Email> email) {
        this.email = email;
    }

}



package pojo;

public class Email {

    private String type;
    private String value;
    
    

    public Email(String type, String value) {
        
        this.type = type;
        this.value = value;
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

在 main 方法中,我使用 -

调用 POJO

PostAccountCreateAPI PostAccountCreateAPIPayLoad = new PostAccountCreateAPI("pri@mail7.io", "P@$$w0rd", "arpita", "garg");

但是它不起作用。谁能建议如何做到这一点?

构造函数的第一个参数是 List<Email>,但您当前传递的是 String 作为构造函数的第一个参数。创建 Email 个对象的列表,然后调用 PostAccountCreateAPI 构造函数。

好的....我在这里给出一个完整的例子:

PostAccountCreateApi class:

import java.util.List;

public class PostAccountCreateAPI{

    private String FirstName;
    private String LastName;
    private String PASSWORD;
    private List<Email> Email;


    public PostAccountCreateAPI(){}

    public PostAccountCreateAPI(String FirstName, String LastName, String PASSWORD, List<Email> Email){

        this.FirstName = FirstName;

        this.LastName = LastName;

        this.PASSWORD = PASSWORD;

        this.Email = Email;

    }

    public void setFirstName(String FirstName){

        this.FirstName = FirstName;

    }

    public String getFirstName(){

        return this.FirstName;

    }

    public void setLastName(String LastName){

        this.LastName = LastName;

    }

    public String getLastName(){

        return this.LastName;

    }

    public void setPASSWORD(String PASSWORD){

        this.PASSWORD = PASSWORD;

    }

    public String getPASSWORD(){

        return this.PASSWORD;

    }

    public void setEmail(List<Email> Email){

        this.Email = Email;

    }
    public List<Email> getEmail(){

        return this.Email;

    }

}

邮箱 Class:

public class Email {

    String TYPE;
    String VALUE;

    public Email() {
    }

    public Email(String TYPE, String VALUE) {

        this.TYPE = TYPE;

        this.VALUE = VALUE;

    }

    public void setTYPE(String TYPE) {

        this.TYPE = TYPE;

    }

    public String getTYPE() {

        return this.TYPE;

    }

    public void setVALUE(String VALUE) {

        this.VALUE = VALUE;

    }

    public String getVALUE() {

        return this.VALUE;
    }
}

主要 class 与虚拟主要方法:


import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        List<Email> emailList = new ArrayList<>();

        emailList.add(new Email("Primary", "pri@mail7.io"));
        emailList.add(new Email("Primary", "amimulahsan7@gmail.com"));

        //And list goes on......


        PostAccountCreateAPI postAccountCreateAPI = new PostAccountCreateAPI("arpita", "garg",
                "P@$$w0rd", emailList);
    }
}