【发布时间】:2019-05-13 05:26:44
【问题描述】:
我是使用 Java 的 Rest-Assured api 的新手。我想提取一个城市的温度高于 18 的天数。使用 openweathermap.org。
网址是 api.openweathermap.org/data/2.5/forecast?q=Sydney&units=metric&appid={APP KEY}
我明白了:
{ “鳕鱼”:“200”, “消息”:0.0067, “cnt”:40, “列表”: [ { “dt”:1557727200, “主要的”: { “温度”:20.88, “temp_min”:20.88, “最高温度”:21.05, “压力”:1025.56, “海平面”:1025.56, “grnd_level”:1021.14, “湿度”:57, “temp_kf”:-0.17 }, “天气”: [ { “身份证”:803, “主”:“云”, "description": "破云", “图标”:“04d” } ], “云”:{ “全部”:55 }, “风”: { “速度”:3.45, “度数”:43.754 }, “系统”:{ “吊舱”:“d” }, “dt_txt”:“2019-05-13 06:00:00” }, { “dt”:1557738000, “主要的”: { “温度”:18.45, “temp_min”:18.45, “最高温度”:18.58, “压力”:1026.06, “海平面”:1026.06, “grnd_level”:1021.28, “湿度”:73, “temp_kf”:-0.13 }, “天气”: [ { “身份证”:804, “主”:“云”, "description": "阴云密布", “图标”:“04n” } ], “云”:{ “全部”:100 }, “风”: { “速度”:3.84, “度数”:28.267 }, “系统”:{ “吊舱”:“n” }, “dt_txt”:“2019-05-13 09:00:00” }, { “DT”:1557759600, “主要的”: { “温度”:14.31, “temp_min”:14.31, “最高温度”:14.35, “压力”:1026.29, “海平面”:1026.29, “grnd_level”:1021.87, “湿度”:80, “temp_kf”:-0.04 }, “天气”: [ { “身份证”:802, “主”:“云”, "description": "散落的云朵", “图标”:“03n” } ], “云”:{ “全部”:28 }, “风”: { “速度”:1.66, “度数”:279.19 }, “系统”:{ “吊舱”:“n” }, “dt_txt”:“2019-05-13 15:00:00” },
}
我不确定如何进行迭代。如果有人可以帮助我完成,那将是一个很大的帮助。
我能够获取数据,但不确定如何从每个数组中获取元素
现在,我想提取 18 度以上的温度
我尝试了以下方法:
public class WeatherForecast {
public static Response resp;
@Test
public void getWeatherForecastForCity()
{
RestAssured.baseURI = "https://api.openweathermap.org";
resp = given().
param("q", "Sydney").
param("units", "metric").
param("appid", "670473f82ba0969a884be548c75236a4").
when().
get("/data/2.5/forecast").
then().
extract().response();
List<String> temperatures = resp.getBody().jsonPath().getList("list");
//String value = temperatures.getString()
int count = temperatures.size();
for(int i=0;i<=count;i++)
{
}
System.out.println("List Size: "+temperatures.size());
//assertEquals(temperatures, greaterThanOrEqualTo(20.00F));
//String responseString = resp.asString();
//System.out.println("Response String: "+responseString);
//JsonPath js = new JsonPath(responseString);
//Get the number of records for the city
//int size = js.getList("list").size();
//int temperature = Integer.parseInt(js.get("count"));
//System.out.println("Temperature Value: "+js.getString("list[1].main.temp"));
}
}
【问题讨论】:
-
检查我的答案。我已经用你提供的 JSON 测试了解决方案,它就像一个魅力
标签: java rest-assured