动态键值对的JSON到JAVA POJO

时间:2016-11-17 14:15:25

标签: java json

我必须创建以下JSON的POJO类,问题是键p_d具有动态名称的变量,如s_t,n_t,n_p等。真正的JSON很大,我只面对该部分的问题,我共享部分JSON。     我正在使用杰克逊进行解析。

{
  "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"
}

1 个答案:

答案 0 :(得分:3)

对于动态键,请使用Map<String, Object>

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