【发布时间】:2011-12-13 03:18:12
【问题描述】:
以下代码在 json 解析器的帮助下获取元素。我的问题是,当我找到 same value 时,我想在 existing hashmap 中放置一个 new element(例如“title2”)一样的价值! 我的第一个想法是在第一个循环之后创建一个新循环(for...)并在此处执行一些操作,但这很难。我找不到有效的方法。 有解决方案吗?有什么想法吗?
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
//Get the elements
HashMap<Integer, String> mapCompare = new HashMap<Integer, String>();
JSONArray data = json.getJSONArray("data");
//Loop the array
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> mapInfo = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
mapInfo.put("id", e.getString("id"));
mapInfo.put("title", e.getString("title"));
mapInfo.put("value", e.getString("value"));
if (mapCompare.containsValue(e.getString("value")) {
mapInfo.put("isSame?", "yes");
} else {
mapInfo.put("isSame?", "no");
}
mapCompare.put(i, e.getString("value"));
listInfo.add(mapInfo);
}
// a new for ??? and how ???
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listInfo;
}
【问题讨论】:
标签: java json algorithm parsing hashmap