【问题标题】:java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: unexpected token: ] @ line 1, column 36. weather[*].descriptionjava.lang.IllegalArgumentException:无效的 JSON 表达式:Script1.groovy:1:意外令牌:] @ 第 1 行,第 36 列。weather[*].description
【发布时间】:2020-08-04 04:36:18
【问题描述】:

我是 RestAssured 的新手并尝试使用以下代码:

    @Test
        public void testJsonPath() {

        Response response = RestAssured

                .given()

                .param("id", "2172797")

                .param("appid", "439d4b804bc8187953eb36d2a8c26a02")

                .when()

                .get("https://samples.openweathermap.org/data/2.5/weather");

            String value = "weather[*].description";    

            System.out.println(value);

            String data = response.then().contentType(ContentType.JSON).extract().path(value);

            System.out.println(data);

        }

JSON:

{
  "coord": {
    "lon": 145.77,
    "lat": -16.92
  },
  "weather": [
    {
      "id": 802,
      "main": "Clouds",
      "description": "scattered clouds",
      "icon": "03n"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 300.15,
    "pressure": 1007,
    "humidity": 74,
    "temp_min": 300.15,
    "temp_max": 300.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 3.6,
    "deg": 160
  },
  "clouds": {
    "all": 40
  },
  "dt": 1485790200,
  "sys": {
    "type": 1,
    "id": 8166,
    "message": 0.2064,
    "country": "AU",
    "sunrise": 1485720272,
    "sunset": 1485766550
  },
  "id": 2172797,
  "name": "Cairns",
  "cod": 200
}

Getting the following error:

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: ] @ line 1, column 36.
                            weather[*].description

为什么在使用 * 时会出现以下错误?替换时 String value = "weather[0].description";它工作正常。有人可以帮我解决这个问题。

我也检查了邮递员,API 给出了正确的输出。

Note: When using creating code (weather[*].description) from http://jsonpath.com/. It gives the same output.

 If there is anything I am missing anything please let me know as I am new to it. Any help to this would be great.

It can also be great if someone can give me a brief on this and let me know whom to refer to get the best output from this.

为什么在使用 * 时会出现以下错误?替换时 String value = "weather[0].description";它工作正常。有人可以帮我解决这个问题。

【问题讨论】:

  • 您应该添加更多标签,以便人们可以找到您的帖子并且代码会被着色。

标签: rest-assured


【解决方案1】:

Rest Assured 使用 Groovy 的 GPath 表示法,不要与 Jayway 的 JsonPath 语法混淆

Official Documentation

GPath

JsonPath

编辑:

{"users":[{"firstName":"sijo","lastName":"john","subjectId":1,"id":1},{"firstName":"sonia","lastName ":"sahay","subjectId":2,"id":2},{"firstName":"shreya","lastName":"sahay","subjectId":1,"id":3}], "subjects":[{"id":1,"name":"Devops"},{"id":2,"name":"SDET"}]}

对于上面的 JSON,你可以使用下面的来获取所有的 lastName

JsonPath abc = new JsonPath(res);
String deal = abc.getString("users.lastName");
System.out.println(deal);

【讨论】:

  • 能否请您添加代码,以便我可以查看我的问题。
  • Rest Assured 使用 GPath,因此您将无法使用 [*] 并且必须将其编码为 [0],或者您也可以使用 .jsonPath().getString("weather.description");
  • 它有效。谢谢。如果我有一个必须全选的 JSON,那么我将如何处理它。 JsonPath 中 * 的替代方法是什么。
  • 例如:{ "users": [ { "firstName": "sijo", "lastName": "john", "subjectId": 1, "id": 1 }, { "firstName ": "sonia", "lastName": "sahay", "subjectId": 2, "id": 2 }, { "firstName": "shreya", "lastName": "sahay", "subjectId": 1, "id": 3 } ], "subjects": [ { "id": 1, "name": "Devops" }, { "id": 2, "name": "SDET" } ] } 如果我想选择所有姓氏,那么我将如何选择。
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
相关资源
最近更新 更多