简体   繁体   中英

convert java.util.ArrayList to gson.JsonElement

Doing some request to the backend API, for receiving some array with JSON objects which I need to use in test run. Using RestAssured+Junit5+Gradle+Allure.

 Response response = 
 given()
    .header("Content-Type", "application/json")
    .body(jsonPayload)
 .when()
    .post(STAGINGSCHEDULE+signature)
 .then()
     .assertThat()
     .statusCode(200)
     .body("message", is("Payload valid"),
     "payment_schedule", hasSize(greaterThan(0)))
     .extract().response();

I would receive this:

ArrayList<JsonElement> jsonElement = response.path("payment_schedule.payment_dates");

This command will show me an array filled with json objects which I needed. But a cound not convert this to the gson.JsonObject.

System.out.println("jsonElement.get(1):   "+jsonElement.get(1));

Which methods I used to usually, when generating data by myself. Object is gsonObject, and array is gsonArray

callbackJournalObject.add("schedule", scheduleArray);

When I try to use

callbackJournalObject.add("schedule", jsonElement.get(1));

I get

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.google.gson.JsonElement

JSON Response body example:

{
    "status": 0,
    "message": "Payload valid",
    "payment_schedule": [
        {
            "total": 16800.0,
            "term": 3,
            "payment_dates": [
                {
                    "date": "16.10.2018",
                    "amount": 5600.0
                },
                {
                    "date": "16.11.2018",
                    "amount": 5600.0
                },
                {
                    "date": "17.12.2018",
                    "amount": 5600.0
                }
            ]
        },
        {
            "total": 16650.0,
            "term": 6,
            "payment_dates": [
                {
                    "date": "16.10.2018",
                    "amount": 2800.0
                },
                {
                    "date": "16.11.2018",
                    "amount": 2800.0
                },
                {
                    "date": "17.12.2018",
                    "amount": 2800.0
                },
                {
                    "date": "16.01.2019",
                    "amount": 2800.0
                },
                {
                    "date": "18.02.2019",
                    "amount": 2800.0
                },
                {
                    "date": "18.03.2019",
                    "amount": 2650.0
                }
            ]
        }
    ]
}

That solution worked for me.

ArrayList<JsonElement> jsonElement = response.path("payment_schedule.payment_dates");
String jsonElementGet1 = String.valueOf(jsonElement.get(1));
scheduleArray =  (JsonArray)jsonParser.parse(jsonElementGet1);

You need to remove the last comma at the end of the JSON as with it, it's not valid JSON. Once you've done that, try again and see if it works. (Used JSONBlob to verify the validity of the JSON).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM