2016-11-17 3 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

이 어디 시도 코드? @Suraj –

+0

공용 클래스 PD { @JsonProperty ("s_t") 개인 목록 sT = new ArrayList (); @JsonProperty ("n_t") 개인 목록 nT = new ArrayList (); @JsonProperty ("n_p") 개인 목록 nP = new ArrayList (); @JsonIgnore private 맵 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 (response, com.HSR.class); \t \t} 캐치 (JsonGenerationException 전자) { \t \t \t e.printStackTrace(); \t \t} – Suraj