繁体   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