0

我对 REST 还很陌生,还无法弄清楚这一点。

我有这样的回应:

{
    "StatusCode": 200,
    "Result": {
        "CustomerStuff": {
            "Name": "John",
            "State": "Oregon",
            "GetEmail": false
        },
        "eText": "Will only get paper mail. "
    }
}

我通常会将响应正文保存为字符串,然后使用 JsonPath 来获取我需要的内容。

String responseBody = given().body().when().etc...;
JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result.CustomerStuff");

然后得到我需要的东西:

String name = jsonPath.get("name");

我不知道如何获得“eText”值。它不在响应的同一部分。

有什么建议么?

4

1 回答 1

1

你应该使用

JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result")

然后调用jsonPath.get("eText")以获得你想要的值。您仍然可以通过以下方式访问 CustomerStuffjsonPath.get("CustomerStuff")

于 2018-03-21T12:20:49.763 回答