我在 POJO 中有 JSON 结构,在 HashMap 中有动态数据 我想将 Hashmap 中的数据设置为 POJO 以发送有效负载,我该如何实现?
I have JSON Structure in POJO and Dynamic data in HashMap I want to set the data from Hashmap to POJO to send the payload, How Can I achieve that?
我有 JSON POJO 结构和 HashMap 动态数据。现在我想将数据从 Hashmap 设置为 POJO 以在 API 中发送有效载荷,我该如何实现?
POJO Class:
package pojo;
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;
}
}
数据存储在 Hashmap 中,如下所示:
{PASSWORD=p@$$word123, LASTNAME=LoUSj, FIRSTNAME=FSFBE,
EMAIL_TYPE=Primary, VALUE=test7EZK0@mail7.io}
如何将 Hashmap
中的值直接分配给 POJO 以创建 API 负载?
可以在 POJO 中提供构造函数来接受映射
public PostAccountCreateAPI(Map<String, String> map) {
this(
map.get("FIRSTNAME"), map.get("LASTNAME"),
map.get("PASSWORD"), Arrays.asList(map.get("VALUE"))
);
}
或一些实用方法:
public class PojoBuilder {
public static PostAccountCreateAPI create(Map<String, String> map) {
return new PostAccountCreateAPI(
map.get("FIRSTNAME"),
map.get("LASTNAME"),
map.get("PASSWORD"),
Arrays.asList(map.get("VALUE"))
);
}
}
更新
如果地图中的键因不同的地图而异,则需要提供键的列表(或数组)以进行迭代,然后假设该列表具有适当顺序的所有相关键:
public static PostAccountCreateAPI create(Map<String, String> map, List<String> keys) {
return new PostAccountCreateAPI(
map.get(keys.get(0)), // key0 - firstName
map.get(keys.get(1)), // key1 - lastName
map.get(keys.get(2)), // key2 - password
Arrays.asList(keys.get(3)) // key3 - email
);
}
我有 JSON POJO 结构和 HashMap 动态数据。现在我想将数据从 Hashmap 设置为 POJO 以在 API 中发送有效载荷,我该如何实现?
POJO Class:
package pojo;
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;
}
}
数据存储在 Hashmap 中,如下所示:
{PASSWORD=p@$$word123, LASTNAME=LoUSj, FIRSTNAME=FSFBE,
EMAIL_TYPE=Primary, VALUE=test7EZK0@mail7.io}
如何将 Hashmap
中的值直接分配给 POJO 以创建 API 负载?
可以在 POJO 中提供构造函数来接受映射
public PostAccountCreateAPI(Map<String, String> map) {
this(
map.get("FIRSTNAME"), map.get("LASTNAME"),
map.get("PASSWORD"), Arrays.asList(map.get("VALUE"))
);
}
或一些实用方法:
public class PojoBuilder {
public static PostAccountCreateAPI create(Map<String, String> map) {
return new PostAccountCreateAPI(
map.get("FIRSTNAME"),
map.get("LASTNAME"),
map.get("PASSWORD"),
Arrays.asList(map.get("VALUE"))
);
}
}
更新
如果地图中的键因不同的地图而异,则需要提供键的列表(或数组)以进行迭代,然后假设该列表具有适当顺序的所有相关键:
public static PostAccountCreateAPI create(Map<String, String> map, List<String> keys) {
return new PostAccountCreateAPI(
map.get(keys.get(0)), // key0 - firstName
map.get(keys.get(1)), // key1 - lastName
map.get(keys.get(2)), // key2 - password
Arrays.asList(keys.get(3)) // key3 - email
);
}