简体   繁体   English

通过JSON 5天天气预报获取有关明天天气的数据

[英]Get data about tomorrow's weather from JSON 5 days forecast

Hello :) I am writing a telegram bot that shows todays and tomorrows weather. 您好:)我正在写一个电报机器人,该机器人显示今天和明天的天气。 As data I'm using openweathermap.org. 作为数据,我正在使用openweathermap.org。

For now I did getTodaysWeather method that get info about Java Objects from JSON on http://www.jsonschema2pojo.org and wrote this: 现在,我做了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";
    }
}

Now I need to write a method that get tomorrow's weather data from this JSON data http://api.openweathermap.org/data/2.5/forecast?q=London&APPID=21f2aefa331b0d3f72e50b14a06ff983 If I think right I need to find the block in data that has date close to "tomorrow 1:00pm" and Get info about temperature, humidity, wind etc. Every block starts with ""dt": 1547672400". 现在,我需要编写一种从JSON数据中获取明天天气数据的方法http://api.openweathermap.org/data/2.5/forecast?q=London&APPID=21f2aefa331b0d3f72e50b14a06ff983如果我认为正确,我需要在数据中找到该块日期接近“明天1:00 pm”,并获取有关温度,湿度,风等的信息。每个块都以““ dt”开头:1547672400”。 I think it means date and time in different format. 我认为这意味着日期和时间的格式不同。 If it so, I need to find the right block and skip the other. 如果是这样,我需要找到正确的块并跳过其他块。

Unfortunately I don't know how to realise this method. 不幸的是,我不知道如何实现这种方法。 If someone could help me I would be very thankful :) 如果有人可以帮助我,我将非常感激:)

What you can do is to convert "tomorrow 1:00pm" to a UTC and then to unix timestamp. 您可以做的是 “明天1:00 pm”转换为UTC,然后转换为unix时间戳。 Store this value as tomorrow13Unix . 将此值存储为tomorrow13Unix

Now for each forecast in the list, you compare it to tomorrow13Unix and if it is the same, you have found what you are looking for. 现在,对于列表中的每个预测,您都可以将其与tomorrow13Unix进行比较,如果相同,则可以找到所需的内容。

Note that the API splits the times into chunks of three, so you may want to instead look for the largest time that is not greater than tomorrow13Unix . 请注意 ,API将时间分为三部分,因此您可能希望查找大于tomorrow13Unix的最大时间。


Here is how I would obtain the epoch time for tomorrow at 1pm: 这是我如何获取明天下午1点的纪元时间:

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

Note 注意

The above will get the current time at UTC, truncate that time to 1pm then add one day. 上面的代码将获得UTC的当前时间,将该时间截断为下午1点,然后添加一天。 This might not be what you want. 这可能不是您想要的。 You may want to instead use your local time, add one day to it, truncate to 1pm, and convert the resulting time to UTC, in which case you may want to use this instead: 您可能要改用本地时间,将其添加一天,将其截断为下午1点,然后将结果转换为UTC,在这种情况下,您可能要改用此时间:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从NetCDF 4.5 Grib2Record提取天气预报数据 - Extract weather forecast data from NetCDF 4.5 Grib2Record 我如何从雅虎 android 获得天气预报 - How i can get weather forecast from yahoo android 如何从JSON对象通道->项目->预测-> {day1} {day2} {day3} {day4} {day5}中获取或获取Yahoo Weather API - how can i retrive or get Yahoo Weather API from JSON object channel -> item -> forecast -> {day1 } {day2} {day3 } {day4} {day5 } 如何从openweathermap API的天气预报数组中获取描述字符串? - How to get the description string from the forecast weather array from the openweathermap API? 从Forecast API服务更改JSON接收的单位数据 - Change JSON received units data from Forecast api service Android如何获得明天的约会 - Android how to get tomorrow's date 使用json和位置显示Weather API中的数据 - Using json and location to display data from a Weather API 如何使用 Retrofit 从 OpenWeatherMap API 获取“天气” object 数据 - How to get “weather” object data from OpenWeatherMap API Using Retrofit 获取明天到未来60天之间的所有时间,然后遍历它们 - Get all days between tomorrow and 60 days forward then loop through them 将JSON数据解析为Java for Android Weather App - Parsing JSON data to Java for Android Weather App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM