在具有变量的JSON响应中提取值

发布于 2025-01-22 14:16:25 字数 704 浏览 5 评论 0原文

我试图从JSON文件中返回声明变量的ID:我希望我的Java代码在JSON响应中搜索以前的变量,并且一旦找到它 - >返回相应的ID。 我正在使用Java重新安排的。

JSON文件响应示例:

[
 {
  "class" : "test 1",
  "id"    : 5,
  "variable" : 87
  },
 {
  "class" : "test 2",
  "id"    : 7,
  "variable" : 76
  }
]

现在,我做类似的事情,但我不知道:

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

String id = String.valueOf(response.
                body().
                jsonPath().
                get("id").toString().contains(variable));

谢谢

I am trying to return the id of a declared variable from a json file: i want my java code to search for the previous variable in the json response and once it is found -> return the corresponding id.
I am using RestAssured in java.

Json file response example:

[
 {
  "class" : "test 1",
  "id"    : 5,
  "variable" : 87
  },
 {
  "class" : "test 2",
  "id"    : 7,
  "variable" : 76
  }
]

Now, i do something like that but i have no idea:

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

String id = String.valueOf(response.
                body().
                jsonPath().
                get("id").toString().contains(variable));

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2025-01-29 14:16:25

在这里快速解决方案,按照GPATH语法查询。

int id = response.jsonPath().get("find {it.variable == 87}.id");

Quick solution here, query by condition with GPath syntax.

int id = response.jsonPath().get("find {it.variable == 87}.id");
桃酥萝莉 2025-01-29 14:16:25

您需要的只是解析您的JSON。这可以非常简单。您可以将给定的JSON解析为list< map>,也可以创建一个简单的类,让我们称其为myClassInfo ,然后将您的JSON解析为list< myclassinfo>。您的myClassInfo看起来像这样:

public class MyClassInfo {
  @JsonProperty("class")
  private String myClass;
  private Integer id;
  private Integer variable;
  // Add setters and getters here
}

现在您可以使用JSON Jackson库轻松解析它:
Formaps:

List<Map<String, Object>> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

并且对于myClassInfo类,

List<MyClassInfo> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

现在您可以浏览列表,然后从每个MAPmyClassInfo中提取所需ID。

为了简化一些简化的内容,您可以使用我写的Mgntutils库中的jsonutils类解析。这是带有jsonutils类的代码(仅适用于myClassInfo class:

    List<MyClassInfo> myList;
    try {
      myList = JsonUtils.readObjectFromJsonString(myJsonString, List.class);
    } catch(IOException ioe) {
    ...
    }

请注意,在这种情况下,您不必实例化和配置objectmapper实例由于ReadObjectFromjSonstring是静态方法。 /mgntutils“ rel =“ nofollow noreferrer”>此处 和源代码和javadoc的库本身在github上此处。 rel =“ nofollow noreferrer”>在这里

All you need is just to parse your Json. This could be done very simply. You can parse your given Json to a List<Map> or you can create a simple class lets call it MyClassInfoand parse your Json to a List<MyClassInfo>. Your MyClassInfo would look like this:

public class MyClassInfo {
  @JsonProperty("class")
  private String myClass;
  private Integer id;
  private Integer variable;
  // Add setters and getters here
}

Now you can easily parse it using Json Jackson library:
ForMaps:

List<Map<String, Object>> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

And for MyClassInfo class

List<MyClassInfo> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

Now you can just go through your list and extract from each map or MyClassInfo the required id.

Just to simplify it a bit more you can do parsing with JsonUtils class from MgntUtils library written by me. Here is the code with JsonUtils class (just for MyClassInfo class:

    List<MyClassInfo> myList;
    try {
      myList = JsonUtils.readObjectFromJsonString(myJsonString, List.class);
    } catch(IOException ioe) {
    ...
    }

Note that in this case you won't have to instantiate and configure ObjectMapper instance as readObjectFromJsonString is a static method. Anyway if you are interested in using my library you can find maven artifacts here and The library itself with source code and javadoc is on Github here. Javadoc for JsonUtils class is here

清风挽心 2025-01-29 14:16:25

我们可以使用JSONPATH获取匹配我们标准的对象列表并返回列表或地图。

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

在这里,我假设变量为class ='test 1'。您可以给出任何标准以与之匹配。

List<Integer> ids = response.jsonpath().get("*[?(@.class=='test 1')]..id");

System.out.println(ids.get(0));

请参阅: https://github.com/json-path/jsonpath/jsonpath

We can use jsonpath to get a list of objects matching our criteria and return a list or map.

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

Here I'm assuming the variable is class='test 1'. You can give any criteria to match against.

List<Integer> ids = response.jsonpath().get("*[?(@.class=='test 1')]..id");

System.out.println(ids.get(0));

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文