I got a rest api which returns me this nested response:
"latitude":37.8267,
"longitude":-122.4233,
"timezone":"America/Los_Angeles",
"minutely":{
"summary":"Clear for the hour.",
"icon":"clear-day",
"data":[
{
"time":1517431560,
"precipIntensity":0,
"precipProbability":0
},
{
"time":1517431620,
"precipIntensity":0,
"precipProbability":0
},
....
So, I need to get minutely weather forecast and put it to the object. I made a HourlyWeather class with getters and setters (not listed):
public class HourlyWeather {
String time;
String precipIntensity;
String precipProbability;
And here are my gherkin steps implemented with java:
@Given("^rest api$")
public void restApi() throws Throwable {
restApiUrl = "https://api.darksky.net/forecast/******"; // my api key
}
@And("^rest api parameters$")
public void restApiParameters() throws Throwable {
restApiUrl = restApiUrl + "/37.8267,-122.4233";
}
@When("^I \"([^\"]*)\" rest api execution result$")
public void iRestApiExecutionResult(String method) throws Throwable {
RestAssured.baseURI = restApiUrl;
RequestSpecification httpRequest = RestAssured.given(); response = httpRequest.request(Method.GET);
}
and here is my question: I'm using rest assured here to get a part of my nested JSON (which I need). And I do a toString conversion here. After that - I'm using GSON to deserialize my string and create a HourlyWeather[] object with all hourly weather json keys data. Is there any way I could avoid this conversion and simplify my code?
@Then("^I should deserialize result just to know how to do that$")
public void iShouldDeserializeResultJustToKnowHowToDoThat() throws Throwable {
// get only part of my nested json
// I would like ot get this part as an array list of HourlyWeather.class objects, not
// as ArrayList of HashMaps, so this is String here
String stringOfRequiredJSON = response.jsonPath().getJsonObject("minutely.data").toString();
Gson gson = new Gson();
HourlyWeather[] hourlyWeatherForecast = gson.fromJson(stringOfRequiredJSON, HourlyWeather[].class);
printHourlyWeather(hourlyWeatherForecast);
}
Thank you!