I have one hashmap inside hashmap like
List<Map> mapList = new ArrayList<Map>();
for (int i = 0; i < 2; i++) {
Map iMap = new HashMap();
iMap.put("Comment", "");
iMap.put("Start", "0");
iMap.put("Max", "0");
iMap.put("Min", "0");
iMap.put("Price", "5000.00");
iMap.put("DetailsID", "51");
mapList.add(iMap);
}
Map mMap = new HashMap();
mMap.put("ID", "27");
mMap.put("ParticipantID", "2");
mMap.put("ItemDetails", mapList);
I want to iterate this map and put in JSONObject for that I do
try {
JSONObject object = new JSONObject();
Iterator iterator = mMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mEntry = (Map.Entry) iterator.next();
String key = mEntry.getKey().toString();
String value = mEntry.getValue().toString();
object.put(key, value);
}
Log.v(TAG, "Object : " + object);
the response comes like
Object : {"ItemDetails":"[{Price=5000.00, Comment=, DetailsID=51, Min=0, Max=0, StartViolation=0}, {Price=5000.00, Comment=, DetailsID=51, Min=0, Max=0, StartViolation=0}]","ID":"27","ParticipantID":"2"}
the inner list of hashmap is not iterating
Indeed. You haven’t written any code to iterate over it. When you get the
ItemDetailsentry, you’ll have a key of"ItemDetails"and a value which is the list. Here’s what you’re then doing with those:So you’re just calling
toString()on the list. You need to work out what you really want to do. For example, you might want:Note that you’ll also probably want to recurse into each
Map. Again, you’ll need to write code to do this. Basically, you can’t assume thattoString()is going to do what you need, which is the assumption you’re making at the moment.