【发布时间】:2021-03-03 07:09:12
【问题描述】:
我想为嵌套了 JSON 的 POST 请求主体创建一个带有构造函数的 POJO 类,但我不知道如何在其中调用 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");
但它不起作用。谁能建议如何做到这一点?
【问题讨论】:
-
在什么情况下它不起作用?你得到什么错误?
标签: java json cucumber bdd pojo