2014-04-03 399 views
2

我有一个有趣的JSON解析问题,至少对我来说,因为我是第一次这样做。我有以下的样品JSON,我想它映射到等价的DTO:将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" 
       }, 
      ] 
     } 
    ] 
} 

这是我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; 
    } 

} 

这里不是复制的,但我PmnDTO类是类似的,即getter和setter对于JSON的pmn对象中的每个属性。

我写了下面的代码,试图将它映射到DTO。我使用的库com.FasterXml.jackson(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); 

很显然,这个代码没有工作。有人可以告诉我,如何将JSON响应映射到我的DTO,因为“模块”是JSON中的数组,并且它本身也包含可变大小的数组。

谢谢。

(* Vipul)();

+0

_明显_并不意味着什么。告诉我们它是如何失败的,它抛出什么异常以及你期望发生什么。 –

+1

您拥有的Json结构不会映射到您的DTO。它匹配ModuleDTO的Array。您可以使用单个属性ArrayList 模块定义ModuleList类;然后将ModuleList.class传递给解析器 – hellboy

+1

3欢呼声给你bellboy :-)我根据你的建议和宾果定义了以下类! ------ package com.sgsi.modulestructure.dto; import java.util.ArrayList; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) public class ModuleListDTO { \t private ArrayList modules; \t public ArrayList getModule(){ \t \t return this。模块; \t} @JsonProperty( “模块”) \t公共无效setModule(ArrayList的模块){ \t \t this.modules =模块; \t} } 谢谢。 – sgsi

回答

2

所有的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" 
       } 
      ] 
     } 
    ] 
} 

然后你可以使用JSON从网上转换成POJO here,你会得到follwing结果:

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

@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.exa mple.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.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; 

@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 ----------- ------------------------

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.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.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); 
} 

} 
+0

这可以在不创建POJO类的情况下完成。我的意思是直接转换为DTO对象。 – warrior107