나는 자바, json, 안심하고 초보자이며 나머지 API를 테스트하는 방법을 배우고 노력하고 있습니다. 안심할 수있는 응답으로 반환 된 배열이 있습니다.
Response response = given(getProjectInfoRequest).get();
response.asString();
{
"options": [
{
"text": "111",
"label": "ABC"
},
{
"text": "222",
"label": "DEF"
},
{
"text": "333",
"label": "GHI"
}
]
}
레이블 값에 대해 "DEF"라는 텍스트 값을 추출하고 싶습니다. 어떻게 할 수 있습니까?
너무 많은 게시물을 읽은 후 지금까지 아래에서 수행했습니다.
1. Options[] options = given(getProjectInfoRequest).when().get().as(Options[].class);
this was giving me exception :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
then I tried below:
2. Options options = gson.fromJson(response.asString(), Options.getClass());
this at least resolved the above issue.
public class Options {
public String getLabel() {
return label
}
public void setLabel(String label) {
this.label = label
}
public String getValue() {
return value
}
public void setValue(String value) {
this.value = value
}
public String label;
public String value;
}
이 시점에서 나는 내가 필요한 것을 추출하기 위해 텍스트와 값의 배열을 어떻게 반복 할 수 있는지 잘 모르겠습니다. 여러분의 입력을 제공 할 수 있습니까?
이런 기본적인 질문에 대한 나의 무지를 용서해주십시오. 이것도 배울 수있는 좋은 소스 / 방법을 제안 해주세요.
미리 감사드립니다!
U can use Gson- 이것은 Java 객체를 JSON 표현으로 변환하는 데 사용할 수있는 Java 라이브러리입니다.
JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse(response.asString());
for (Map.Entry<String,JsonElement> entry : o.entrySet()) {
JsonArray array = entry.getValue().getAsJsonArray();
for (JsonElement elementJSON : array) {
[...]
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다