java - 如何在放心中获得大于指定值的值
问题描述
我是使用 Java 的 Rest-Assured api 的新手。我想提取一个城市温度高于 18 的天数。使用 openweathermap.org。
网址是 api.openweathermap.org/data/2.5/forecast?q=Sydney&units=metric&appid={APP KEY}
我得到:
{ "cod": "200", "message": 0.0067, "cnt": 40, "list": [ { "dt": 1557727200, "main": { "temp": 20.88, "temp_min": 20.88, “temp_max”:21.05,“压力”:1025.56,“sea_level”:1025.56,“grnd_level”:1021.14,“湿度”:57,“temp_kf”:-0.17 },“天气”:[{“id”:803, “main”:“云”,“description”:“破云”,"icon": "04d" } ], "clouds": { "all": 55 }, "wind": { "speed": 3.45, "deg": 43.754 }, "sys": { "pod": " d" }, "dt_txt": "2019-05-13 06:00:00" }, { "dt": 1557738000, "main": { "temp": 18.45, "temp_min": 18.45, "temp_max": 18.58,“压力”:1026.06,“海平面”:1026.06,“grnd_level”:1021.28,“湿度”:73,“temp_kf”:-0.13 },“天气”:[{“id”:804,“main”:“云”,“描述”:“阴云”,“图标": "04n" } ], "clouds": { "all": 100 }, "wind": { "speed": 3.84, "deg": 28.267 }, "sys": { "pod": "n" }, "dt_txt": "2019-05-13 09:00:00"},{“dt”:1557759600,“main”:{“temp”:14.31,“temp_min”:14.31,“temp_max”:14.35,“压力”:1026.29,“sea_level”:1026.29,“grnd_level”:1021.87, “湿度”:80,“temp_kf”:-0.04 },“天气”:[ { “id”:802,“主要”:“云”,“描述”:“散落的云”,“图标”:“03n” } ],“云”:{“全部”:28},“风”:{“速度”:1.66,“度”:279.19},“系统”:{“pod”:“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"));
}
}
解决方案
RestAssured
JsonPath
使用您已经使用过的功能强大的库。
您已经创建的不同之处在于如何在其他 JSONObject/Array 中获取 JSONObject/Array
您使用resp.getBody().jsonPath().getList("list");
了这几乎是一个很好的起点。而不是使用List<String>
你应该使用List<HashMap<String, Object>>
每个HashMap<>
都是 JSON 对象。
然后,您可以遍历 Array 以获取每个对象和温度。
怎么做:
List<HashMap<String, Object>> list = resp.getBody().jsonPath().getList("list");
for (HashMap<String, Object> jsonObject : list) {
/**
* Now, in order to get temperature, we have to get access to `main` element in JSON and then get access to the temperature
**/
HashMap<String, Object> mainElements = (HashMap<String, Object>) jsonObject.get("main");
//No we have JSONObject as HashMap. We can access any temperature
float temperature = (float) mainElements.get("temp");
System.out.println(temperature);
}
上面的代码将打印所有温度。现在你只需要比较浮点值,保存它们,断言它们或做任何你想做的事情:)
您可以使用这种方法访问任何值
编辑: 将上面的代码提取到这样的方法中:
private List<HashMap<String, Object>> getJsonObjectsWithTempGreaterThan(JsonPath path, float degrees) {
List<HashMap<String, Object>> desiredJsonObjects = new ArrayList<>();
List<HashMap<String, Object>> list = path.getList("list");
for (HashMap<String, Object> jsonObject : list) {
HashMap<String, Object> mainElements = (HashMap<String, Object>) jsonObject.get("main");
float temperature = (float) mainElements.get("temp");
if (temperature > degrees) {
desiredJsonObjects.add(jsonObject);
}
}
return desiredJsonObjects;
}
上面的代码会将每个 JSON 对象存储在一个 List 中,然后返回它。该列表将包含温度高于参数中传递的度数的 JSON 对象。
然后,您可以像这样访问这些元素:
List<HashMap<String, Object>> objects = getJsonObjectsWithTempGreaterThan(path, 20);
这是我们想要的对象列表。如果你只想要温度,你所要做的就是:
for (HashMap<String, Object> jsonObject : objects) {
HashMap<String, Object> mainElements = (HashMap<String, Object>) jsonObject.get("main");
//No we have JSONObject as HashMap. We can access any temperature
float temperature = (float) mainElements.get("temp");
System.out.println(temperature);
}
使用 Java Streams 可以更容易和更易读地实现这一点。
推荐阅读
- python - Python,文本到语音,无效语法,使用 IDLE 编辑器
- spring-security - 覆盖 isAccountNotLocked 方法的 UserDetailsChecker 实现
- python - 如何从 Python 上的外部文本文档打印排序列表?
- php - 从 Ajax 请求中获取 JSON 并在 Leaflet 中创建多边形层
- javascript - 高阶组件看不到道具
- google-chrome-extension - 从 chrome 扩展加载的 iframe 的 CSP 问题
- sql-server - SSIS 使用 SQLCMD 执行进程任务错误
- python - Pyplot中colorbar.solids的含义是什么?
- java - 如何在查询中引用属性值?
- libcurl - 我无法让 libcurl 在特定的 url 中正常工作