2016-04-04 134 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。