簡體   English   中英

無法從嵌套的JSON路徑提取數據

[英]Unable to extract the data from the Nested JSON path

我是API自動化的新手。 這是我的JSON結構。 我想從所有3個數組中提取“ LunId”。 代碼中提到了路徑。

JSON結構1
JSON擴展結構1

錯誤1:

在代碼中使用“ $”時

錯誤2:

在代碼中不使用“ $”時

@Test

public void getAllLuns(){   

Response resp = given().header("Authorization", "Bearer "+"ddnsdndnn22x");
                when().
                get("http://10.100.242.11/api/v1/luns");

String Name =  resp.                
               then().
               contentType(ContentType.JSON).
               extract().
               path("$..LunId").toString();

System.out.println("Lun Name is--->"+LunId);

我在JSON路徑提取器中在線提取正確的數據,但是在eclipse中運行它卻給我一個錯誤。 問題出在路上。 請幫助,如何使用這些類型的路徑。 在路徑中使用“ $”時獲取Error1-,在不使用時獲取Error2。

使用JSONPath Lib可以輕松完成此操作

https://github.com/json-path/JsonPath

   public List<String> getLunIDNames() {

    return lunIDNamess = JsonPath.read("ValidJSON", "$..LunId[*].Name");

}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM