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:
Saurabh Kedia 2019-02-22 20:07:06 +05:30 committed by Josh Cummings
parent 13d63ad4e6
commit 0d94b49731
3 changed files with 136 additions and 0 deletions

View File

@ -73,6 +73,12 @@
<version>${commons-collections4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
@ -86,6 +92,7 @@
<jackson-databind.version>2.9.7</jackson-databind.version>
<junit.version>4.12</junit.version>
<javax.version>1.1.2</javax.version>
<assertj-core.version>3.11.1</assertj-core.version>
</properties>
</project>

View File

@ -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;
}
}

View File

@ -0,0 +1,79 @@
package com.baeldung.jsonobject.iterate;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
public class JSONObjectIteratorUnitTest {
private JSONObjectIterator jsonObjectIterator = new JSONObjectIterator();
@Test
public void givenJSONObject_whenIterating_thenGetKeyValuePairs() {
JSONObject jsonObject = getJsonObject();
jsonObjectIterator.handleJSONObject(jsonObject);
Map<String, Object> keyValuePairs = jsonObjectIterator.getKeyValuePairs();
assertThat(keyValuePairs.get("rType")).isEqualTo("Regular");
assertThat(keyValuePairs.get("rId")).isEqualTo("1001");
assertThat(keyValuePairs.get("cType")).isEqualTo("Chocolate");
assertThat(keyValuePairs.get("cId")).isEqualTo("1002");
assertThat(keyValuePairs.get("bType")).isEqualTo("BlueBerry");
assertThat(keyValuePairs.get("bId")).isEqualTo("1003");
assertThat(keyValuePairs.get("name")).isEqualTo("Cake");
assertThat(keyValuePairs.get("cakeId")).isEqualTo("0001");
assertThat(keyValuePairs.get("type")).isEqualTo("donut");
assertThat(keyValuePairs.get("Type")).isEqualTo("Maple");
assertThat(keyValuePairs.get("tId")).isEqualTo("5001");
assertThat(keyValuePairs.get("batters")
.toString()).isEqualTo("[{\"rType\":\"Regular\",\"rId\":\"1001\"},{\"cType\":\"Chocolate\",\"cId\":\"1002\"},{\"bType\":\"BlueBerry\",\"bId\":\"1003\"}]");
assertThat(keyValuePairs.get("cakeShapes")
.toString()).isEqualTo("[\"square\",\"circle\",\"heart\"]");
assertThat(keyValuePairs.get("topping")
.toString()).isEqualTo("{\"Type\":\"Maple\",\"tId\":\"5001\"}");
}
private JSONObject getJsonObject() {
JSONObject cake = new JSONObject();
cake.put("cakeId", "0001");
cake.put("type", "donut");
cake.put("name", "Cake");
JSONArray batters = new JSONArray();
JSONObject regular = new JSONObject();
regular.put("rId", "1001");
regular.put("rType", "Regular");
batters.put(regular);
JSONObject chocolate = new JSONObject();
chocolate.put("cId", "1002");
chocolate.put("cType", "Chocolate");
batters.put(chocolate);
JSONObject blueberry = new JSONObject();
blueberry.put("bId", "1003");
blueberry.put("bType", "BlueBerry");
batters.put(blueberry);
JSONArray cakeShapes = new JSONArray();
cakeShapes.put("square");
cakeShapes.put("circle");
cakeShapes.put("heart");
cake.put("cakeShapes", cakeShapes);
cake.put("batters", batters);
JSONObject topping = new JSONObject();
topping.put("tId", "5001");
topping.put("Type", "Maple");
cake.put("topping", topping);
return cake;
}
}