BAEL-2223: Processing JSONArray (#5547)
This commit is contained in:
committed by
maibin
parent
e31557b2dc
commit
44cef7b602
@@ -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")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user