Manthan Patel
Manthan Patel

Reputation: 1083

Parse json data with retrofit

I got dynamic number of key in json object it will be change according to data i am using retrofit

JSON

{
"response_message": "Settings has been displayed successfully",
"deliveryCharges": [
  {
    "iSettingId": 2,
    "vSettingDescription": "deliveryCharges",
    "vSettingName1": 1,
    "vSettingName2": 1.5,
    "vSettingName3": 1.75,
    .
    .
    .
    "vSettingNamen": 1.75
  },
  {
    "iSettingId": 3,
    "vSettingDescription": "deliveryCharges",
    "vSettingName1": 1,
    "vSettingName2": 1.5,
    "vSettingName3": 1.75,
    .
    .
    .
    "vSettingNamen": 1.75
  },
  {
    "iSettingId": 4,
    "vSettingDescription": "deliveryCharges",
    "vSettingName1": 1,
    "vSettingName2": 1.5,
    "vSettingName3": 1.75,
    .
    .
    .
    "vSettingNamen": 1.75
  }
]
}

here i can get n number of vSettingName. It is totally dynamic so how can i make POJO for this

Upvotes: 0

Views: 326

Answers (1)

Shweta Patel
Shweta Patel

Reputation: 728

Try with this

Gson gson = new Gson();
Type mapType = new TypeToken<List<Map<String, String>>>(){}.getType(); //define generic type
List<Map<String, String>> result = gson.fromJson(deliveryCharges,mapType);

Upvotes: 1

Related Questions