如何将地图转换为对象?

时间:2019-10-03 18:46:54

标签: java hashmap apache-commons-beanutils

我制作了一个Map,将所有标记的值存储在SOAPMessage主体上,以便Map的键是节点名,而值是文本内容。我有一个对象,其中已经有以节点名称命名的字段,我需要做的是根据其对应的地图设置它们的值。

假设我在SOAPMessage上有一个名为“ Summary”的节点,将有一个名为“ Summary”的Map键以及一个名为“ Summary”的对象字段。我需要将对象字段“摘要”设置为Map.get(“摘要”)的值。

我知道我可以使用每个字段的设置器来填充代码,但是有没有办法从地图上设置整个对象?

这是我创建地图的方法。

private static Map<String, String> mapIncidentInfo(SOAPMessage soapResponse) throws SOAPException {

    Map<String, String> fields = new HashMap<String, String>();
    NodeList nodes = soapResponse.getSOAPBody().getFirstChild().getChildNodes();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        fields.put(node.getNodeName(), node.getTextContent());
    }

    return fields;
}

这可以用作对象类示例:

public class IncidentPO {

    private String Submitter;
    private String Submit_Date;
    private String Last_Modified_By;
    private String Last_Modified_Date;
    private String Status;
    private String Short_Description;

    public IncidentPO(String Submitter, String Submit_Date, String Last_Modified_By, String Last_Modified_Date, String Status, String Short_Description) {
    this.Submitter                      = Submitter;
    this.Submit_Date                    = Submit_Date;
    this.Last_Modified_By               = Last_Modified_By;
    this.Last_Modified_Date             = Last_Modified_Date;
    this.Status                         = Status;
    this.Short_Description              = Short_Description;

    //getters and setters here

2 个答案:

答案 0 :(得分:0)

没有简单的方法(没有库)将Map转换为对象。一种直接的选择是在构造函数中提供Map并使其自身填充。

public IncidentPO(Map<String, String> map) {
    this.Submitter = map.get("Submitter");
    this.Submit_Date = map.get("Submit_Date");
    // etc
}

答案 1 :(得分:0)

您可以使用对象到json的映射,然后再次使用json到对象,如下所示:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class TestMap {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        Map<String, String> myMmap = new HashMap<>();
        myMmap.put("name", "ABC");
        myMmap.put("age", "20");
        myMmap.put("sex", "male");
        myMmap.put("city", "madhepura");
        myMmap.put("spec", "java");

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode objectNode1 = mapper.createObjectNode();
        for(String key : myMmap.keySet()) {
            objectNode1.put(key, myMmap.get(key));
        }
        // take the value of objectNode1.toString() and create a pojo from http://www.jsonschema2pojo.org/
        Person person = mapper.readValue(objectNode1.toString().getBytes(), Person.class);
        System.out.println(person);
    }

}

//您可以使用http://www.jsonschema2pojo.org从objectNode1.toString()获取POJO。

// {“ city”:“ patna”,“ sex”:“ male”,“ name”:“ ABC”,“ age”:“ 20”,“ spec”:“ java”}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"city",
"sex",
"name",
"age",
"spec"
})
class Person {

@JsonProperty("city")
private String city;
@JsonProperty("sex")
private String sex;
@JsonProperty("name")
private String name;
@JsonProperty("age")
private String age;
@JsonProperty("spec")
private String spec;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("city")
public String getCity() {
return city;
}

@JsonProperty("city")
public void setCity(String city) {
this.city = city;
}

@JsonProperty("sex")
public String getSex() {
return sex;
}

@JsonProperty("sex")
public void setSex(String sex) {
this.sex = sex;
}

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("age")
public String getAge() {
return age;
}

@JsonProperty("age")
public void setAge(String age) {
this.age = age;
}

@JsonProperty("spec")
public String getSpec() {
return spec;
}

@JsonProperty("spec")
public void setSpec(String spec) {
this.spec = spec;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}