【问题标题】:Get data about tomorrow's weather from JSON 5 days forecast从 JSON 5 天预报中获取有关明天天气的数据
【发布时间】:2019-06-10 05:44:30
【问题描述】:

你好 :) 我正在编写一个显示今天和明天天气的电报机器人。作为数据,我使用的是 openweathermap.org。

现在我确实使用 getTodaysWeather 方法从 http://www.jsonschema2pojo.org 上的 JSON 获取有关 Java 对象的信息并写了这个:

public class Weather {

    public static final String URL_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q=";
    public static final String API_KEY = "&APPID=3ad54740fd37f3f14a3a32a09f09cd25";
    public static final String UNITS = "&units=metric";
    public static final String LANG = "&lang=ru";

    public static String getWeather(String message) throws IOException{

        URL url = new URL(URL_SOURCE + message + LANG + UNITS + API_KEY);

        InputStreamReader reader = new InputStreamReader(url.openStream());

        Scanner in = new Scanner((InputStream) url.getContent());
        String result = "";

        while (in.hasNext()) {
            result += in.nextLine();
        }

        OneDayWeather obj = null;
        Gson gson = new Gson();
        String json = result;

        obj = gson.fromJson(json, OneDayWeather.class);

        System.out.println("City " + obj.getName() + "(" + obj.getSys().getCountry()+ ")" + "today's "+ System.lineSeparator() +
                "Temperature: " + obj.getMain().getTemp() + "°C, " + System.lineSeparator()+
                "Humidity: " + obj.getMain().getHumidity() + "%, " + System.lineSeparator()+
                "Rain: " + obj.getWeather().get(0).getDescription()+ System.lineSeparator()+
                "Wind speed: " + obj.getWind().getSpeed() + " m/s";
    }
}

现在我需要编写一个方法,从这个 JSON 数据中获取明天的天气数据 http://api.openweathermap.org/data/2.5/forecast?q=London&APPID=21f2aefa331b0d3f72e50b14a06ff983 如果我认为正确,我需要找到日期接近“明天下午 1:00”的数据块,并获取有关温度、湿度、风等的信息。 每个块都以 ""dt": 1547672400" 开头。我认为这意味着不同格式的日期和时间。如果是这样,我需要找到正确的块并跳过另一个。

不幸的是我不知道如何实现这个方法。 如果有人可以帮助我,我将非常感激:)

【问题讨论】:

  • 您应该正确阅读 API 文档,他们似乎有一个端点要求进行 5 天预测
  • API Docs 看起来很清楚:openweathermap.org/forecast5#JSON - list.dt Time of data forecasted, unix, UTC
  • 你似乎知道如何解析 JSON - 这个特定结构到底有什么问题?
  • 我确实读过它。不知道怎么写代码找到“明天13:00”
  • dt 是 UTC 的 unix 时间。如果您不知道那是什么,请查找unix time

标签: java json datetime openweathermap


【解决方案1】:

您可以将convert "tomorrow 1:00pm" 转换为 UTC,然后转换为 unix 时间戳。将此值存储为tomorrow13Unix

现在,对于列表中的每个预测,您将其与 tomorrow13Unix 进行比较,如果相同,则您已找到所需内容。

请注意,API 将时间分成三个部分,因此您可能希望寻找大于tomorrow13Unix 的最大时间。


这是我如何获得明天下午 1 点的纪元时间:

long tomorrow13Unix = java.time.OffsetDateTime.now(java.time.ZoneOffset.UTC).with(java.time.LocalTime.of(13, 0)).plusDays(1).toEpochSecond();

注意

以上内容将获取 UTC 的当前时间,将该时间截断为下午 1 点,然后添加一天。这可能不是你想要的。您可能想改用您的本地时间,添加一天,截断为下午 1 点,然后将结果时间转换为 UTC,在这种情况下,您可能想改用它:

long tomorrow13Unix = java.time.LocalDateTime.now().plusDays(1).with(java.time.LocalTime.of(13, 0)).toEpochSecond(java.time.ZoneOffset.UTC);

【讨论】:

  • 不幸的是,我不能只设置“1 月 17 日下午 1:00”。该方法应执行以下操作:1)获取当前日期 2)添加 +1 天 3)在接近“当前日期 +1 天”+ 1:00 pm 的 JSON unix 日期时间带中搜索。这就是我的想法
  • 我做的是:Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); calendar.add(Calendar.DAY_OF_YEAR, 1); Date tomorrow = calendar.getTime(); System.out.println(tomorrow);
  • 我想我需要编写方法来查找明天 1:00 pm(今天是 17.01.2019 1:00 pm,明天它将是 18.01.2019 1:00 pm)。但是怎么做呢?
  • @MichaelBlala 避免使用已弃用的日期实用程序。我更新了我的答案以显示如何获取第二天的时间(注意使用推荐的java.time.* utilties)
猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2016-01-27
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2019-08-08
  • 2019-05-28
  • 1970-01-01
相关资源
最近更新 更多