java: json parser problem with same strings
I wrote this code:
private ArrayList<HashMap<String, HashMap<Integer, String>>> listContent2 = new ArrayList<HashMap<String, HashMap<Integer, String>>>();
public ArrayList<HashMap<String, HashMap<Integer, String>>> content() {
JSONObject json = JSONfunctions.getJSONfromURL("http://...");
try {
JSONArray hotspots = json.getJSONArray("hotspots");
HashMap<String, HashMap<Integer, String>> mapContentHotspot = new HashMap<String, HashMap<Integer, String>>();
for (int i = 0; i < hotspots.length(); i++) {
JSONObject e = hotspots.getJSONObject(i);
JSONArray actions = new JSONArray(e.getString("actions"));
for (int j = 0; j < actions.length(); j++) {
JSONObject e2 = actions.getJSONObject(j);
HashMap<Integer, String> mapContent = new HashMap<Integer, String>();
switch (e2.getInt("activityType")) {
case 27:
mapContent.put(e2.getInt("activityType"),开发者_如何学Python e2.getString("uri"));
case 2:
mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
case 1:
mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
//default:
//break;
}
mapContentHotspot.put(e.getString("id"), mapContent);
}
listContent2.add(mapContentHotspot);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listContent2;
}
to take the content of this json array: http://pastebin.com/bXfwcQ2U The problem is in the section of "actions". I try to take the 3 "uri" but i take only the last one (with the "activityType": "1"). Where is my wrong in my java code? thanks!
Maybe mapContent creation should be moved outside of the innermost loop.
You have to add break !!! in your code
case 27:
mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
break;
case 2:
mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
break;
case 1:
mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
break;
精彩评论