【问题标题】:How do I get a value greater than a specified value in rest assured如何在放心中获得大于指定值的值
【发布时间】: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


【解决方案1】:

RestAssured使用一个强大的库,名为JsonPath,您已经使用过。

您已经创建的不同之处在于如何在其他 JSONObject/Array 中获取 JSONObject/Array

您使用了resp.getBody().jsonPath().getList("list");,这几乎是一个很好的起点。而不是使用List&lt;String&gt;,你应该使用List&lt;HashMap&lt;String, Object&gt;&gt;

每个HashMap&lt;&gt; 都是一个 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);
}

上面的代码将打印所有温度。现在你只需要比较浮点值,保存它们,断言它们或者做任何你想做的事情:)

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

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

    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 可以更轻松地实现这一点并且可读性更强。

【讨论】:

  • 哇。这就像一个魅力。感谢那。我的意图是获得温度大于 20 度的天数
  • @Amit 您需要其他帮助吗?如果没有,请接受我的回答:)
  • 是的,可以。请告诉我该怎么做。
  • @Amit 再次检查:)
  • 非常感谢。今天学到了新东西。我现在需要对线程做些什么吗,比如接受或关闭或其他什么?不确定 。任何指针,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多