Skip to content

Commit 44cef7b

Browse files
shubhi22maibin
authored andcommitted
BAEL-2223: Processing JSONArray (eugenp#5547)
1 parent e31557b commit 44cef7b

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jsonjava;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
8+
import org.json.JSONArray;
9+
import org.json.JSONObject;
10+
11+
public class JSONArrayGetValueByKey {
12+
13+
public List<String> getValuesByKeyInJSONArray(String jsonArrayStr, String key) {
14+
List<String> values = new ArrayList<>();
15+
JSONArray jsonArray = new JSONArray(jsonArrayStr);
16+
for (int idx = 0; idx < jsonArray.length(); idx++) {
17+
JSONObject jsonObj = jsonArray.getJSONObject(idx);
18+
values.add(jsonObj.optString(key));
19+
}
20+
return values;
21+
}
22+
23+
public List<String> getValuesByKeyInJSONArrayUsingJava8(String jsonArrayStr, String key) {
24+
JSONArray jsonArray = new JSONArray(jsonArrayStr);
25+
return IntStream.range(0, jsonArray.length())
26+
.mapToObj(index -> ((JSONObject) jsonArray.get(index)).optString(key))
27+
.collect(Collectors.toList());
28+
}
29+
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.jsonjava;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertThat;
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
11+
public class JSONArrayGetValueByKeyUnitTest {
12+
13+
private static final JSONArrayGetValueByKey obj = new JSONArrayGetValueByKey();
14+
15+
@Test
16+
public void givenJSONArrayAndAKey_thenReturnAllValuesForGivenKey() {
17+
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
18+
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
19+
20+
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
21+
22+
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
23+
}
24+
25+
@Test
26+
public void givenJSONArrayAndAKey_whenUsingJava8Syntax_thenReturnAllValuesForGivenKey() {
27+
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
28+
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
29+
30+
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
31+
32+
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)