我必须修改 JSONObject 中键的每个值,并且我使用递归来迭代 JSONObject,因为它可以嵌套。但是我遇到了异常,it.next()
因为我试图用 foreach 循环、fori 和 foreach 改变方法,但无法解决。
Iterator<String> it = myList.iterator();
while (it.hasNext()) {
String value = it.next();
jsonObject.put(value, newValue);
}
完整的递归方法是这样的:
public void handleJSON(Object input, Map<String,Object> result ){
if (input instanceof JSONObject) {
for (String key: ((JSONObject) input).keySet()) {
if (!(((JSONObject) input).get(key) instanceof JSONArray))
if (((JSONObject) input).get(key) instanceof JSONObject) {
handleJSONObject(((JSONObject) input).get(key), map);
} else {
Object value = ((JSONObject) input).get(key);
((JSONObject) input).put(key, handleValue(value, map));
}
else
handleJSONObject(new JSONArray(((JSONObject) input).get(key).toString()), map);
}
}
if (input instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) input).length(); i++) {
JSONObject jsonObject = ((JSONArray) input).getJSONObject(i);
handleJSONObject(jsonObject, map);
}
}
}