BAEL-2223: Processing JSONArray (#5547)

This commit is contained in:
Shubhra Srivastava
2018-10-27 21:32:16 +05:30
committed by maibin
parent e31557b2dc
commit 44cef7b602
2 changed files with 65 additions and 0 deletions
@@ -0,0 +1,35 @@
package com.baeldung.jsonjava;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
public class JSONArrayGetValueByKeyUnitTest {
private static final JSONArrayGetValueByKey obj = new JSONArrayGetValueByKey();
@Test
public void givenJSONArrayAndAKey_thenReturnAllValuesForGivenKey() {
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
}
@Test
public void givenJSONArrayAndAKey_whenUsingJava8Syntax_thenReturnAllValuesForGivenKey() {
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
}
}