首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >天气应用编程接口的JSon日期解析

天气应用编程接口的JSon日期解析
EN

Stack Overflow用户
提问于 2017-06-20 00:17:29
回答 2查看 769关注 0票数 1

我一直在分析我的天气数据我可以成功地提取描述,最低和最高温度,但日期是未知的格式如何处理日期将其转换为可读格式

代码语言:javascript
复制
  "list":[  
  {  
     "dt":1497852000,
     "temp":{  
        "day":301.14,
        "min":294.81,
        "max":301.14,
        "night":294.81,
        "eve":301.14,
        "morn":301.14
     },
"pressure":990.68,
     "humidity":88,
     "weather":[  
        {  
           "id":501,
           "main":"Rain",
           "description":"moderate rain",
           "icon":"10d"
        }

我的代码:

代码语言:javascript
复制
 public static void JSONParsing(String forecastJsonStr) throws JSONException {
    double MinTemp;
    double MaxTemp;
    String Date,description;
    JSONObject forecastDate = new JSONObject(forecastJsonStr);
    JSONArray ForecastData = forecastDate.getJSONArray("list");
    for(int i =0 ; i< ForecastData.length();i++){
        JSONObject weather = ForecastData.getJSONObject(i);
        JSONObject Data = weather.getJSONObject("temp");
        JSONObject Description = weather.getJSONArray("weather").getJSONObject(0);
         description = Description.getString("description");
        MinTemp = Data.getDouble("min");
        MaxTemp = Data.getDouble("max");


    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-06-20 00:23:07

您的日期是seconds格式的long类型,因此请尽可能长时间地获取dt,然后将其与1000相乘,以将其转换为miliseconds,使用DateSimpleDateFormat

1.)获取您的日期作为long

2.)将其传递给Date类构造函数,同时将其与1000相乘,以将秒转换为毫秒

3.)创建并应用SimpleDateFormat来获取日期

e.g

代码语言:javascript
复制
    String s1 ="{\"dt\":1497852000}";
    JSONObject jsonObject2 = new JSONObject(s1);

    java.util.Date date = new java.util.Date(jsonObject2.getLong("dt")*1000);
    SimpleDateFormat date_format = new SimpleDateFormat("dd/MM/yy");
    String dateText = date_format.format(date);
    System.out.println(dateText); 

输出:

代码语言:javascript
复制
19/06/17

注意:由于您的JSONResponse不完整,所以我只添加了一个简单的案例来演示您的问题

票数 1
EN

Stack Overflow用户

发布于 2017-06-20 02:21:25

你可以像这样格式化,

代码语言:javascript
复制
    try {
        JSONObject responseObject = new JSONObject(response);


        JSONArray listJsonArray = responseObject.optJSONArray("list");

        if (listJsonArray == null || listJsonArray.length() == 0) {
            return;
        }

        for (int i = 0; i < listJsonArray.length(); i++) {
            JSONObject eachDayJson = listJsonArray.optJSONObject(i);

            if (eachDayJson == null) {
                continue;
            }

            long dateInSeconds = eachDayJson.optLong("dt"); // Get date as seconds

            SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm");

            String readableDate = format.format(new Date(dateInSeconds * 1000)); // convert that in milliseconds

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44635433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档