这是我第一次在这里发布问题:
我有这种 json 结构:
"{ "element": ["
+ " {"
+ " "name": "name1",n"
+ " "value": "Married"n"
+ " },"
+ " {"
+ " "name": "name2",n"
+ " "value": 0n"
+ " },"
+ " {"
+ " "name": "name3",n"
+ " "value": 0n"
+ " }"
+ " ] }"
生成的 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 结构作为我的示例?
似乎你只是忘记了它生成的第二个类。对于您的 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);
}
}
所以实际上类Example
是你想要的POJO,它包含Element
POJO的列表。 Element
是为数组中的对象类型生成的 POJO。请注意,该类称为 Example
,因为这是 jsonschema2pojo.org 设置的顶级对象的默认名称。您可以在右侧的输入字段中进行更改。