简体   繁体   English

无法从嵌套的JSON路径提取数据

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

I am new to API automation. 我是API自动化的新手。 Here is my JSON structure. 这是我的JSON结构。 I want to extract the "LunId" from all the 3 arrays. 我想从所有3个数组中提取“ LunId”。 Path is mentioned in the code. 代码中提到了路径。

JSON结构1
JSON扩展结构1

Error1: 错误1:

在代码中使用“ $”时

Error2: 错误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);

I am extracting the correct data in JSON path Extractor online, but while running it in eclipse it gives me an error. 我在JSON路径提取器中在线提取正确的数据,但是在eclipse中运行它却给我一个错误。 Problem is in the path. 问题出在路上。 Please help, how to work with these kinds of paths. 请帮助,如何使用这些类型的路径。 Getting Error1- when using"$" in the path and Error2 when not using it. 在路径中使用“ $”时获取Error1-,在不使用时获取Error2。

This can easily be done using JSONPath Lib 使用JSONPath Lib可以轻松完成此操作

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

   public List<String> getLunIDNames() {

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

}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM