在放心的情况下,如何获得大于指定值的值

时间:2019-05-13 05:26:45

标签: java rest-assured

我是使用Java的Rest-Assured api的新手。我想提取一个城市的温度高于18天的天数。使用openweathermap.org。

网址为api.openweathermap.org/data/2.5/forecast?q=Sydney&units=metric&appid={APP KEY}

我明白了:

  

{       “ cod”:“ 200”,       “消息”:0.0067,       “ cnt”:40,       “列表”:[           {               “ dt”:1557727200,               “主要”:{                   “温度”:20.88,                   “ temp_min”:20.88,                   “ temp_max”:21.05,                   “压力”:1025.56,                   “海平面”:1025.56,                   “ grnd_level”:1021.14,                   “湿度”:57,                   “ temp_kf”:-0.17               },               “天气”:[                   {                       “ id”:803,                       “ main”:“云”,                       “ description”:“碎云”,                       “ icon”:“ 04d”                   }               ],               “云”:{                   “全部”:55               },               “风”:{                   “速度”:3.45,                   “度”:43.754               },               “ sys”:{                   “ pod”:“ d”               },               “ dt_txt”:“ 2019-05-13 06:00:00”           },           {               “ dt”:1557738000,               “主要”:{                   “温度”: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”:“云”,                       “ description”:“阴云”,                       “ icon”:“ 04n”                   }               ],               “云”:{                   “全部”:100               },               “风”:{                   “速度”:3.84,                   “度”:28.267               },               “ sys”:{                   “ pod”:“ n”               },               “ dt_txt”:“ 2019-05-13 09:00:00”           },{               “ dt”:1557759600,               “主要”:{                   “温度”:14.31,                   “ temp_min”:14.31,                   “ temp_max”:14.35,                   “压力”:1026.29,                   “海平面”:1026.29,                   “ grnd_level”:1021.87,                   “湿度”:80,                   “ temp_kf”:-0.04               },               “天气”:[                   {                       “ id”:802,                       “ main”:“云”,                       “ description”:“分散的云”,                       “ icon”:“ 03n”                   }               ],               “云”:{                   “全部”:28               },               “风”:{                   “速度”:1.66,                   “度”:279.19               },               “ sys”:{                   “ 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"));

    }

}

1 个答案:

答案 0 :(得分:2)

RestAssured使用一个功能强大的JsonPath库,您已经在使用它。

您已经创建的内容的区别在于如何在其他JSONObject / Array中获取JSONObject / Array

您使用了resp.getBody().jsonPath().getList("list");,这几乎是一个很好的起点。您应该使用List<String>

而不是使用List<HashMap<String, Object>>

每个HashMap<>将是一个JSON对象。

然后,您可以遍历数组以获取每个对象和温度。

操作方法:

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);
}

以上代码将打印所有温度。现在,您只需要比较float值,保存它们,声明它们或执行您想要的任何事情:)

您可以使用此方法访问任何值

编辑: 将上面的代码提取到这样的方法中:

    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对象存储在一个列表中,然后将其返回。该列表将包含温度高于参数中传递的度数的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轻松实现,并且可读性强。