由jsonschema2pojo.org为Json对象生成奇怪的Pojo

时间:2014-02-27 23:09:33

标签: java json jackson

这是我第一次在这里发帖提问:

我有这种类型的json结构:

    "{  \"element\": ["
        + "    {"
        + "      \"name\": \"name1\",\n"
        + "      \"value\": \"Married\"\n"
        + "    },"
        + "    {"
        + "      \"name\": \"name2\",\n"
        + "      \"value\": 0\n"
        + "    },"
        + "    {"
        + "      \"name\": \"name3\",\n"
        + "      \"value\": 0\n"
        + "    }"
        + "  ] }"

生成的POJO如下所示:

public class Element {
    private String name;
    private String value;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

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

如果POJO指示只有一个名称值属性,我理解只需设置这些属性,就像element.setName(),element.setValue一样。

但是如何在这个POJO中设置值,以便以Json结构为例?

1 个答案:

答案 0 :(得分:1)

你似乎忘记了它产生的第二堂课。对于你的json:

{
    "element": [ 
      { "name": "name1",
        "value": "Married"
      }, 
      { "name": "name2",
        "value": 0
      },
      { "name": "name3",
        "value": 0
      }  ]
}

该工具http://www.jsonschema2pojo.org/生成以下代码(设置为Jackson 2.x注释和源类型JSON时):

-----------------------------------com.example.Elemant.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"name",
"value"
})
public class Elemant {

@JsonProperty("name")
private String name;
@JsonProperty("value")
private Integer value;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

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

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

@JsonProperty("value")
public Integer getValue() {
return value;
}

@JsonProperty("value")
public void setValue(Integer value) {
this.value = value;
}

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

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

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"element"
})
public class Example {

@JsonProperty("element")
private List<Elemant> element = new ArrayList<Elemant>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("element")
public List<Elemant> getElement() {
return element;
}

@JsonProperty("element")
public void setElement(List<Elemant> element) {
this.element = element;
}

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

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

}

实际上,它是您想要的POJO类Example,它包含Element POJO的列表。 Element是为数组中的对象类型生成的POJO。请注意,该类称为Example,因为它是jsonschema2pojo.org设置的顶级对象的默认名称。您可以在右侧的输入字段中更改它。