java .lang.IllegalArgumentException: недопустимо JSON выражение: Script1. groovy: 1: неожиданный токен:] @ строка 1, столбец 36. погода [*]. Описание - PullRequest
0 голосов
/ 21 апреля 2020

Я новичок в 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"; это работает отлично. Может кто-нибудь, пожалуйста, помогите мне с этим.

1 Ответ

1 голос
/ 21 апреля 2020

Rest Assured использует нотацию Groovy GPath и его не следует путать с синтаксисом JsonPath Jayway

Официальная документация

GPath

JsonPath

Редактировать:

{"users": [{"firstName": "sijo" , "LastName": "Джон", "subjectId": 1, "идентификатор": 1}, { "FirstName": "Сонечка", "LastName": "Sahay", "subjectId": 2, "идентификатор": 2 }, { "FirstName": "Shreya", "LastName": "Sahay", "subjectId": 1, "идентификатор": 3}], "субъекты": [{ "ID": 1, "название":» Devops "}, {" id ": 2," name ":" SDET "}]}

Для приведенного выше JSON вы можете использовать приведенное ниже, чтобы получить все lastName

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