Java 将 Json 转换为 DTO 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22849897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Json into a DTO array
提问by sgsi
I have an interesting JSON parsing problem, at least to me since I am doing this for the first time. I have the following sample JSON and I want to map it to equivalent DTOs:
我有一个有趣的 JSON 解析问题,至少对我来说,因为我是第一次这样做。我有以下示例 JSON,我想将它映射到等效的 DTO:
{
"modules":
[
{
"name":"module1",
"shortId":23425,
"pmns":
[
{
"name":"pmn1",
"position":1,
"pmnType":"D3"
},
{
"name":"pmn3",
"position":3,
"pmnType":"R2"
},
{
"name":"pmn7",
"position":5,
"pmnType":"S1"
},
]
},
{
"name":"module2",
"shortId":1572,
"pmns":
[
{
"name":"pmn1",
"position":3,
"pmnType":"D3"
},
{
"name":"pmn12",
"position":35,
"pmnType":"R2"
},
]
}
]
}
This is my ModuleDTO class:
这是我的 ModuleDTO 类:
public class ModuleDTO {
private String _name;
private short _shortId;
private PmnDTO[] _pmns;
public String getName() {
return _name;
}
public short getShortId() {
return _shortId;
}
public PmnDTO[] getPmns() {
return _pmns;
}
@JsonProperty("name")
public void setName(String name) {
this._name = name;
}
@JsonProperty("shortId")
public void setShortId(short shortId) {
this._shortId = shortId;
}
@JsonProperty("pmns")
public void setPmns(PmnDTO[] pmns) {
this._pmns = pmns;
}
}
Not copied here but my PmnDTO class is similar, i.e. getters and setters for each property in the pmn object of JSON.
这里没有复制,但我的 PmnDTO 类是类似的,即 JSON 的 pmn 对象中每个属性的 getter 和 setter。
I wrote the following code to try to map it to DTO. The library I am using is com.FasterXml.Hymanson (version 2.3.1)
我编写了以下代码来尝试将其映射到 DTO。我使用的库是 com.FasterXml.Hymanson(版本 2.3.1)
// Got the response, construct a DTOs out of it ...
ObjectMapper mapper = new ObjectMapper();
StringReader reader = new StringReader(response); // Json Response
// Convert the JSON response to appropriate DTO ...
ModuleDTO moduleDto = mapper.readValue(reader, ModuleDTO.class);
Obviously, this code didn't work. Can someone tell, how can I map the JSON response to my DTOs, given that "modules" is an array in the JSON and it also contains a variable size array within itself.
显然,这段代码不起作用。有人能告诉我如何将 JSON 响应映射到我的 DTO,因为“模块”是 JSON 中的一个数组,并且它本身也包含一个可变大小的数组。
Thank You.
谢谢你。
(*Vipul)() ;
(*Vipul)() ;
回答by Paulo Fidalgo
First of all your JSON is invalid, so I suspect you want this instead:
首先你的 JSON 无效,所以我怀疑你想要这个:
{
"modules": [
{
"name": "module1",
"shortId": 23425,
"pmns": [
{
"name": "pmn1",
"position": 1,
"pmnType": "D3"
},
{
"name": "pmn3",
"position": 3,
"pmnType": "R2"
},
{
"name": "pmn7",
"position": 5,
"pmnType": "S1"
}
]
},
{
"name": "module2",
"shortId": 1572,
"pmns": [
{
"name": "pmn1",
"position": 3,
"pmnType": "D3"
},
{
"name": "pmn12",
"position": 35,
"pmnType": "R2"
}
]
}
]
}
Then you can use the online conversion from JSON to POJO hereand you'll get the follwing result:
然后你可以在这里使用从 JSON 到 POJO 的在线转换,你会得到以下结果:
-----------------------------------com.example.Example.java-----------------------------------
-------------------------------------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 javax.validation.Valid;
import com.fasterxml.Hymanson.annotation.JsonAnyGetter;
import com.fasterxml.Hymanson.annotation.JsonAnySetter;
import com.fasterxml.Hymanson.annotation.JsonIgnore;
import com.fasterxml.Hymanson.annotation.JsonInclude;
import com.fasterxml.Hymanson.annotation.JsonProperty;
import com.fasterxml.Hymanson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"modules"
})
public class Example {
@JsonProperty("modules")
@Valid
private List<Module> modules = new ArrayList<Module>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("modules")
public List<Module> getModules() {
return modules;
}
@JsonProperty("modules")
public void setModules(List<Module> modules) {
this.modules = modules;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Module.java-----------------------------------
--------------------- com.example.Module.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 javax.validation.Valid;
import com.fasterxml.Hymanson.annotation.JsonAnyGetter;
import com.fasterxml.Hymanson.annotation.JsonAnySetter;
import com.fasterxml.Hymanson.annotation.JsonIgnore;
import com.fasterxml.Hymanson.annotation.JsonInclude;
import com.fasterxml.Hymanson.annotation.JsonProperty;
import com.fasterxml.Hymanson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"name",
"shortId",
"pmns"
})
public class Module {
@JsonProperty("name")
private String name;
@JsonProperty("shortId")
private Integer shortId;
@JsonProperty("pmns")
@Valid
private List<Pmn> pmns = new ArrayList<Pmn>();
@JsonIgnore
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("shortId")
public Integer getShortId() {
return shortId;
}
@JsonProperty("shortId")
public void setShortId(Integer shortId) {
this.shortId = shortId;
}
@JsonProperty("pmns")
public List<Pmn> getPmns() {
return pmns;
}
@JsonProperty("pmns")
public void setPmns(List<Pmn> pmns) {
this.pmns = pmns;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Pmn.java-----------------------------------
------------------------------------- com.example.Pmn.java-------- ---------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.Hymanson.annotation.JsonAnyGetter;
import com.fasterxml.Hymanson.annotation.JsonAnySetter;
import com.fasterxml.Hymanson.annotation.JsonIgnore;
import com.fasterxml.Hymanson.annotation.JsonInclude;
import com.fasterxml.Hymanson.annotation.JsonProperty;
import com.fasterxml.Hymanson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"name",
"position",
"pmnType"
})
public class Pmn {
@JsonProperty("name")
private String name;
@JsonProperty("position")
private Integer position;
@JsonProperty("pmnType")
private String pmnType;
@JsonIgnore
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("position")
public Integer getPosition() {
return position;
}
@JsonProperty("position")
public void setPosition(Integer position) {
this.position = position;
}
@JsonProperty("pmnType")
public String getPmnType() {
return pmnType;
}
@JsonProperty("pmnType")
public void setPmnType(String pmnType) {
this.pmnType = pmnType;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}

