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
the inner list of hashmap is not iterating
Indeed. You haven't written any code to iterate over it. When you get the ItemDetails
entry, you'll have a key of "ItemDetails"
and a value which is the list. Here's what you're then doing with those:
String key = mEntry.getKey().toString();
String value = mEntry.getValue().toString();
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:
if (mEntry.getValue() instanceof List) {
// Handle lists here, possibly recursively
}
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 that toString()
is going to do what you need, which is the assumption you're making at the moment.
try to do like this:
Set<Map.Entry<String, String>> entrySet = JSONObject.entrySet();
for (Entry entry : entrySet) {
// your code
}
for (Map.Entry<String, String> entry : JSONObject.entrySet()) {
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.