我想從影像中突出顯示的 JSON 中獲取值。 任何人都可以幫助我,我怎樣才能建立一條路徑來獲得這些價值。使用我擁有的代碼,它會回傳所有 id,但我只需要陣列中的第二個物件。有關更多詳細資訊,我在影像中留下了一個示例。
JSON 結構
我正在使用 Restassured 來做到這一點。
public static List<String> JSON_UltimateParent(String parent) {
baseURI = uri;
List<List<String>> LinkedListUltimate =
given()
.auth().basic(getJiraUser(), getJiraPass())
.param("limit", "74000000")
.param("count", "false")
.param("sort", "accountId")
.when()
.get("/counterparties.json?" parent)
.then()
.extract().path("riskUltimateParent.identifier.id");
List<String> ultimates = linkedList_To_List(LinkedListUltimate);
return ultimates;
}
This method make the parse to list
public static List<String> linkedList_To_List(List<List<String>> response){
List<String> accountIds = response.stream().flatMap(l-> l.stream()).collect(Collectors.toList());
return accountIds;
}
uj5u.com熱心網友回復:
螢屏截圖中共享的 JSON 應該是有效的。以下是有效版本:
{
"riskUltimateParent" :
[{
"name": "Test1",
"identifier" : [{"id":1,"type":"abcd1"},{"id":2,"type":"abcd2"},{"id":3,"type":"abcd3"}]
},
{
"name": "Test2",
"identifier" : [{"id":4,"type":"abcd4"},{"id":4,"type":"abcd5"},{"id":6,"type":"abcd6"}]
}]
}
您正在尋找的 JSON 路徑應該是 extract().path("riskUltimateParent[:].identifier[1]");
如果您需要 id 或輸入:
extract().path("riskUltimateParent[:].identifier[1].id");
extract().path("riskUltimateParent[:].identifier[1].type");
uj5u.com熱心網友回復:
我使用 jsonpath jayway,這是示例代碼:
String res = given()...asString();
List<String> ids = JsonPath.read(res, "$..identifier[?(@.type == 'CLIENTREF')].id");
System.out.println(ids);
//["AVIVANVS","DAVIDSNKC"]
pom.xml
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.6.0</version>
</dependency>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314031.html
標籤:爪哇 json 休息 放心 放心的jsonpath
