BAEL-2720 Iterating over an org.json.JSONObject (#6326)
* BAEL-2720: Iterating over an org.json.JSONObject * BAEL-2720 Iterating over an org.json.JSONObject * http://jira.baeldung.com/browse/BAEL-2720 * http://jira.baeldung.com/browse/BAEL-2720 * BAEL-2720: Iterating over an org.json.JSONObject * BAEL-2720 Iterating over an org.json.JSONObject * BAEL-2720 Iterating over an org.json.JSONObject
This commit is contained in:
committed by
Josh Cummings
parent
13d63ad4e6
commit
0d94b49731
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.jsonobject.iterate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class JSONObjectIterator {
|
||||
|
||||
private Map<String, Object> keyValuePairs;
|
||||
|
||||
public JSONObjectIterator() {
|
||||
keyValuePairs = new HashMap<>();
|
||||
}
|
||||
|
||||
public void handleValue(String key, Object value) {
|
||||
if (value instanceof JSONArray) {
|
||||
handleJSONArray(key, (JSONArray) value);
|
||||
} else if (value instanceof JSONObject) {
|
||||
handleJSONObject((JSONObject) value);
|
||||
}
|
||||
keyValuePairs.put(key, value);
|
||||
}
|
||||
|
||||
public void handleJSONObject(JSONObject jsonObject) {
|
||||
Iterator<String> jsonObjectIterator = jsonObject.keys();
|
||||
jsonObjectIterator.forEachRemaining(key -> {
|
||||
Object value = jsonObject.get(key);
|
||||
handleValue(key, value);
|
||||
});
|
||||
}
|
||||
|
||||
public void handleJSONArray(String key, JSONArray jsonArray) {
|
||||
Iterator<Object> jsonArrayIterator = jsonArray.iterator();
|
||||
jsonArrayIterator.forEachRemaining(element -> {
|
||||
handleValue(key, element);
|
||||
});
|
||||
}
|
||||
|
||||
public Map<String, Object> getKeyValuePairs() {
|
||||
return keyValuePairs;
|
||||
}
|
||||
|
||||
public void setKeyValuePairs(Map<String, Object> keyValuePairs) {
|
||||
this.keyValuePairs = keyValuePairs;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user