简体   繁体   中英

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?

I have JSON Structure in POJO and dynamic data in HashMap. Now I want to set the data from Hashmap to POJO to send the payload in API, How Can I achieve that?

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;
    }
}

The Data is stored in a Hashmap as below:

{PASSWORD=p@$$word123, LASTNAME=LoUSj, FIRSTNAME=FSFBE, 
EMAIL_TYPE=Primary, VALUE=test7EZK0@mail7.io}

How can I assign the values from Hashmap to POJO directly to create API Payload?

A constructor may be provided in the POJO to accept a map

public PostAccountCreateAPI(Map<String, String> map) {
    this(
        map.get("FIRSTNAME"), map.get("LASTNAME"), 
        map.get("PASSWORD"), Arrays.asList(map.get("VALUE"))
    );
}

or some utility method:

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"))
        );
    }
}

Update

If the keys in the map vary for different maps, the list (or array) of the keys needs to be provided to iterate on, then assuming that the list have all relevant keys in appropriate order:

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
    );
}

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