2016-04-04 12 views
0

以下の構造のJSONデータがあります。同じ構造と同じ名前のPOJOを作成しようとしました。私は、次のJSONで数値オブジェクトの構造を持つDTOのリストと文字列 "通知"を含むDTOを取得しました。私は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

jsonを解析するために使用しているPOJOを表示します。 – dambros

+0

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

+0

public class RenewalAPICallListDTO { \tプライベートマップ apiCallList; \tパブリックマップ getApiCallList(){ \t \t return apiCallList; \t} \t公共ボイドsetApiCallList(MAP <整数、JSONCallDto> apiCallList){ \t \t this.apiCallList = apiCallList。 \t} \tプライベート文字列通知; \t \t公開ストリングgetNotice(){ \t \t返品通知; \t} \tパブリックvoid setNotice(文字列通知){ \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をハッシュマップに変換しました。 マップデータ= mapper.readValue(json、Map.class); を作成し、マップを反復してオブジェクトを使用してPOJOを設定します。

関連する問題