使用剩余保证来验证Json对象中的值

问题描述 投票:0回答:1

我想确认ID:1属于老虎尼克松

{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""}

我一直在关注此文档:https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath,本节body("shopping.category.find { it.@type == 'groceries' }.item", hasItems("Chocolate", "Coffee"));

当前,我得到一个空指针异常。我可以使用一些帮助来提出解决方案。预先感谢您的宝贵时间。

import com.google.gson.JsonObject;
import io.restassured.RestAssured;
import org.testng.Assert;

public class RestAssuredExample_2 {

    public void getResponse() {
        JsonObject responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees")
                .then()
                .extract().response().as(JsonObject.class);

        String emp1Name = responseObject.get("data.find{it.@id=='1'}.employee_name").toString();
        Assert.assertEquals(emp1Name, "Tiger Nixon");
    }

    public static void main(String[] args) {
        RestAssuredExample_2 rest2 = new RestAssuredExample_2();
        rest2.getResponse();
    }
}  

错误:

Exception in thread "main" java.lang.NullPointerException
    at HTTPz.RestAssuredExample_2.getResponse(RestAssuredExample_2.java:16)
java json rest-assured
1个回答
0
投票

试图复制这种情况

String responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees").then().extract().asString();
JsonPath js = new JsonPath(responseObject);
String emp1Name = js.get("data.find {it.id =='1'}.employee_name").toString();
System.out.println(emp1Name);

并且我将emp1Name的值作为“ Tiger Nixon”]

Difference:

原始: it.@id=='1'

Mine: it.id =='1'

似乎@是用于XMLPath,而不是JSONPath,老实说,我也不会详细了解它,因为我使用JSONPath的时间很长了:)

© www.soinside.com 2019 - 2024. All rights reserved.