2016-04-04 29 views
0

我有以下结构的JSON数据。我曾尝试创建具有相同结构和相同名称的POJO。我带了一个DTO,其中包含一个列表(DTO的结构中的数字对象在下面的JSON中)和一个String“Notice”。我无法获得DTO中的数据。无法解析JSON数据到POJO

{ 
    "notice": "This API is in a pre-launch state, and will go through significant changes.", 
    "1": { 
     "next_renewal_date": "2014-08-01", 
     "next_renewal_fee": { 
      "price": "800.0", 
      "currency": "USD" 
     }, 
     "next_renewal_description": "1st Annuity - Official Fee", 
     "next_per_claim_fee": { 
      "price": "0.0", 
      "currency": "USD", 
      "free_claims": 0, 
      "claim_type": "claims_count" 
     }, 
     "next_agent_fee": { 
      "price": "0.0", 
      "currency": "USD" 
     }, 
     "grace_period_end_date": "2015-02-01" 
    }, 
    "2": { 
     "next_renewal_date": "2018-08-01", 
     "next_renewal_fee": { 
      "price": "1800.0", 
      "currency": "USD" 
     }, 
     "next_renewal_description": "2nd Annuity - Official Fee", 
     "next_per_claim_fee": { 
      "price": "0.0", 
      "currency": "USD", 
      "free_claims": 0, 
      "claim_type": "claims_count" 
     }, 
     "next_agent_fee": { 
      "price": "0.0", 
      "currency": "USD" 
     }, 
     "grace_period_end_date": "2019-02-01" 
    } 
} 

POJO:

public class RenewalAPICallListDTO { 
    private Map<Integer,JSONCallDto> apiCallList; 

    public Map<Integer, JSONCallDto> getApiCallList() { 
     return apiCallList; 
    } 
    public void setApiCallList(Map<Integer, JSONCallDto> apiCallList) { 
     this.apiCallList = apiCallList; 
    } 
    private String notice; 

    public String getNotice() { 
     return notice; 
    } 
    public void setNotice(String notice) { 
     this.notice = notice; 
    } 
} 

方法调用:

Gson gson = new Gson(); 
RenewalAPICallListDTO respDto = gson.fromJson(response1.toString(), RenewalAPICallListDTO.class); 
+0

显示你的POJO和你用来解析json的任何东西。 – dambros

+0

Gson gson = new Gson(); RenewalAPICallListDTO respDto = gson.fromJson(response1.toString(),RenewalAPICallListDTO.class); –

+0

public class RenewalAPICallListDTO { \t private Map apiCallList; \t public Map getApiCallList(){ \t \t return apiCallList; \t} \t公共无效setApiCallList(地图<整数,JSONCallDto> apiCallList){ \t \t this.apiCallList = apiCallList; \t} \t private String notice; \t \t public String getNotice(){ \t \t return notice; (字符串通知){ \t \t this.notice = notice; \t} \t \t } –

回答

0

什么你正在寻找能与杰克逊可以实现一个自定义解串器,如下所示:

public class CustomDeserializer extends JsonDeserializer<RenewalAPICallListDTO> { 

    private static final ObjectMapper mapper = new ObjectMapper(); 

    static { 
     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
    } 

    @Override 
    public RenewalAPICallListDTO deserialize(JsonParser jp, DeserializationContext ctxt) 
      throws IOException { 


     JsonNode node = jp.getCodec().readTree(jp); 

     Iterator<Entry<String, JsonNode>> nodeIterator = node.fields(); 

     RenewalAPICallListDTO dto = new RenewalAPICallListDTO(); 
     Map<Integer, JsonCallDto> map = new HashMap<>(); 

     while (nodeIterator.hasNext()) { 
      Map.Entry<String, JsonNode> entry = nodeIterator.next(); 
      if (entry.getKey().equalsIgnoreCase("notice")) { 
       dto.setNotice(entry.getValue().toString()); 
      } else { 
       map.put(Integer.parseInt(entry.getKey()), mapper.readValue(entry.getValue().toString(), JsonCallDto.class)); 
      } 
     } 
     dto.setApiCallList(map); 
     return dto; 
    } 

} 

用法:

public static void main(String[] args) throws Exception { 

    ObjectMapper mapper = new ObjectMapper(); 
    SimpleModule module = new SimpleModule(); 
    module.addDeserializer(RenewalAPICallListDTO.class, new CustomDeserializer()); 
    mapper.registerModule(module); 

    RenewalAPICallListDTO dto = mapper.readValue(JSON, RenewalAPICallListDTO.class); 
} 

最后dto将被正确序列化像你想,即使已经设置了正确的类型。

+0

谢谢@dambros! –

0

的JSON和POJO不匹配。您的JSON字符串中缺少apiCallList属性。

的结构应该是这样的:

{ 
    "notice": "random string", 
    "apiCallList": { 
     "1": { 
      "next_renewal_date": "2014-08-01", 
      ... 
     }, 
     "2": { 
      "next_renewal_date": "2014-08-01", 
      .... 
     } 
    } 
} 
+0

是的,这应该已经解决了这个问题,但我不能改变JSON。 –

0

我已经找到一种方法。谢谢您的帮助。 我已经将JSON转换为Hashmap使用: Map data = mapper.readValue(json,Map.class); 然后迭代地图,使用对象来填充POJO。