2016-11-17 17 views
-2

JSONの次のPOJOクラスを作成する必要があります。問題はキーp_dにs_t、n_t、n_pなどの動的名を持つ変数があります。実際のJSONは大きくてその部分の問題に直面して、私は部分JSONを共有しました。 私はjacksonを解析に使用しています。動的キーと値のペアのJSONからJAVA POJO

動的なキーについては
{ 
    "flag": true, 
    "flag2": false, 
    "r_no": [ 
    { 
     "room_type": 250067, 
     "no_of_rooms": 1, 
     "no_of_children": 1, 
     "no_of_adults": 2, 
     "description": "Executive Room, 1 King Bed, Non Smoking", 
     "children_ages": [ 
     8 
     ] 
    }, 
    { 
     "room_type": 250067, 
     "no_of_rooms": 1, 
     "no_of_children": 0, 
     "no_of_adults": 2, 
     "description": "Executive Room, 1 King Bed, Non Smoking" 
    } 
    ], 
    "r_code": "abc", 
    "r_key": "123", 
    "p_d": { 
    "s_t": [ 
     { 
     "name": "xyz", 
     "cur": "INR" 
     }, 
     { 
     "name": "xyz1", 
     "cur": "INR" 
     } 
    ], 
    "n_t": [ 
     { 
     "name": "xyz2", 
     "cur": "INR" 
     } 
    ], 
    "n_p": [ 
     { 
     "name": "xyz5", 
     "cur": "INR" 
     } 
    ] 
    }, 
    "cur": "INR" 
} 
+0

どこのtryコードはありますか? @Suraj –

+0

パブリッククラスPD { @JsonProperty( "s_t") 民間一覧 ST =新しいArrayListを(); @JsonProperty( "n_t") プライベートリスト nT = new ArrayList (); @JsonProperty( "n_p") プライベートリスト nP = new ArrayList (); @JsonIgnore プライベートマップ additionalProperties = new HashMap (); setter getter} – Suraj

+0

ここでは変数名は静的ですが、動的変数が必要です。 – Suraj

答えて

3

Map<String, Object>を使用します。

ObjectMapper mapper = new ObjectMapper(); 
Map<String, Object> parsed = mapper.readValue(json, 
            new TypeReference<Map<String, Object>>() {}); 
+0

同じことをやっていますが、JSONは大きく、問題に直面している部分だけを共有しています。ObjectMapper mapper = new ObjectMapper(); \t \t試み{ \t \t \t responseDto = mapper.readValue(応答、com.HSR.class)。 \t \t}キャッチ(JsonGenerationException電子){ \t \t \t e.printStackTrace(); \t \t} – Suraj

関連する問題