Reputation: 173
i have to construct a json request with dynamic key below is the sample i need to create, i have set "dynamic_key" from a response which i get it from other service and "dynamic_value" from user interaction, i'm not sure how to change the "dynamic_key" value everytime, could someone tell me how to do it. TIA
"qty":{
"dynamic_key":"dynamic_value"
}
Upvotes: 2
Views: 1768
Reputation: 539
Map it is in hashmap. then get all keys from that hashmap and get values on base of that keys. For example: Map map; String is your key and object is your values. Sample Code:
Map<String, Object> map = response.getQTYData();
List<QTY> mapList = new ArrayList<>();
Set mapSet = map.entrySet();
Iterator mapIterator = mapSet.iterator();
Map.Entry mapEntry;
while (mapIterator.hasNext()) {
mapEntry = (Map.Entry) mapIterator.next();
String key = (String) mapEntry.getKey();
QTY qty = new QTY();
qty.setKey(key);
qty.setValue((String)map.get(key));
mapList.add(paymentMethod);
}
Upvotes: 0
Reputation: 5375
JSONObject object = new JSONObject();
object.put("dynamic_key", "dynamic_value");
Upvotes: 2