/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
json-modules/json/src/test/java/com/baeldung/jsonjava/JSONArrayGetValueByKeyUnitTest.java
35 строк
1 KB
panos-kakos
Java 11497 (#12399)
24 июн 2022, 19:28
24 июн 2022, 19:28
a51caf5
Код
Авторство
О чём код?
package com.baeldung.jsonjava; import org.junit.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; 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) .containsExactlyInAnyOrder("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) .containsExactlyInAnyOrder("John", "Gary", "Selena"); } }