安卓JSON解析问题。

安卓 这种JSON怎么解析?

只需要得到天气、温度等信息

{"HeWeather data service 3.0":[{"basic":{"city":"漯河","cnty":"中国","id":"CN101181501","lat":"33.563000","lon":"114.026000","update":{"loc":"2015-09-15 14:46","utc":"2015-09-15 06:46"}},"daily_forecast":[{"astro":{"sr":"06:06","ss":"18:31"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-15","hum":"39","pcpn":"0.1","pop":"1","pres":"1016","tmp":{"max":"28","min":"13"},"vis":"10","wind":{"deg":"62","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:07","ss":"18:30"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-16","hum":"42","pcpn":"0.0","pop":"0","pres":"1016","tmp":{"max":"28","min":"16"},"vis":"10","wind":{"deg":"74","dir":"东北风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:28"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-17","hum":"41","pcpn":"0.0","pop":"0","pres":"1017","tmp":{"max":"27","min":"17"},"vis":"10","wind":{"deg":"137","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:27"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-18","hum":"36","pcpn":"0.0","pop":"1","pres":"1015","tmp":{"max":"28","min":"17"},"vis":"10","wind":{"deg":"117","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:09","ss":"18:26"},"cond":{"code_d":"101","code_n":"100","txt_d":"多云","txt_n":"晴"},"date":"2015-09-19","hum":"35","pcpn":"0.0","pop":"0","pres":"1014","tmp":{"max":"29","min":"17"},"vis":"10","wind":{"deg":"67","dir":"东北风","sc":"3-4","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:24"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-20","hum":"31","pcpn":"0.0","pop":"0","pres":"1011","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"121","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:23"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-21","hum":"30","pcpn":"0.0","pop":"0","pres":"1010","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"157","dir":"无持续风向","sc":"微风","spd":"0"}}],"hourly_forecast":[{"date":"2015-09-15 16:00","hum":"39","pop":"0","pres":"1015","tmp":"30","wind":{"deg":"60","dir":"东北风","sc":"微风","spd":"12"}},{"date":"2015-09-15 19:00","hum":"53","pop":"0","pres":"1015","tmp":"27","wind":{"deg":"68","dir":"东北风","sc":"微风","spd":"14"}},{"date":"2015-09-15 22:00","hum":"67","pop":"0","pres":"1017","tmp":"23","wind":{"deg":"72","dir":"东北风","sc":"微风","spd":"11"}}],"now":{"cond":{"code":"100","txt":"晴"},"fl":"26","hum":"38","pcpn":"?","pres":"1018","tmp":"27","vis":"10","wind":{"deg":"62","dir":"东北风","sc":"4-5","spd":"0"}},"status":"ok","suggestion":{"comf":{"brf":"较舒适","txt":"白天天气晴好,您在这种天气条件下,会感觉早晚凉爽、舒适,午后偏热。"},"cw":{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},"drsg":{"brf":"热","txt":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"},"flu":{"brf":"少发","txt":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"},"sport":{"brf":"较适宜","txt":"天气较好,但考虑气温较低,推荐您进行室内运动,若户外适当增减衣物并注意防晒。"},"trav":{"brf":"适宜","txt":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},"uv":{"brf":"强","txt":"紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。"}}}]}

我安卓新人 只会解析像
[{"id":"5","version":"3.1","name":"Angry Birds"},{"id":"6","version":"6.1","name":"nate"},{"id":"7","version":"4.1","name":"youdao"}]
这样的JSON,现在试着做一个天气app,已得到这个JSON,就是不会解析,我百度了好久都弄不好-.- 特此来求

阅读 7.4k
4 个回答

哈哈,我看了一下你关注了我,然后顺便看了一下你的几个问题,我最近也在弄一个天气app。
真巧我也用的这个api,不过还是劝你换一个,这个不好,经常换数据,我就栽里面了,bug调了一下午才发现是这个api的问题。。。

解析这种数量大的json内容,使用GSON会比较轻松,那本书里应该有的。这里有个链接使用Gson解析复杂的json数据的方法
看完之后一般复杂的json数据你都会用gson解析了,可是"HeWeather data service 3.0"这种奇葩还是不会。
这个时候你就要用SerializedName了。搜索一下我相信你能够找到用法。
要是你用的是android studio的话,可以装个插件GSONFormat,一键生成实体类,更加方便。

我也是新手,共勉

我们把json文件重新排列,目的是为了呈现其清晰的嵌套结构。因此,可以注意到,json包含这些元素:
[]括起来的称之为数组JSONArray;
{}括起来的称之为JSONObject;
字符串String
boolean, double, int, long 基本数据类型;

通过JSON Object的方法获取这些数据,比如string:JSONObject.getString(String name)
然后根据JSON文件的结构,一层一层地解析:

public void parseWeatherObject(JSONObject object) throws JSONException  {
    if (object != null) {
        JSONArray array = object.getJSONArray("HeWeather data service 3.0");
        if (array != null && array.length() > 0) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject weatherItem = array.getJSONObject(i);
                JSONObject basic = weatherItem.getJSONObject("basic");
                JSONArray dailyForecast = weatherItem.getJSONArray("daily_forecast");
                JSONArray hourly_forecast = weatherItem.getJSONArray("hourly_forecast");
                JSONObject now = weatherItem.getJSONObject("now");
                // parse "now"
                parseNowObject(now);
                String status = weatherItem.getString("status");
                JSONObject suggestion = weatherItem.getJSONObject("suggestion");
                i=i;
            }
        }
    }
}

private void parseNowObject(JSONObject now) throws JSONException {
    if (now != null) {
        JSONObject cond = now.getJSONObject("cond");
        int condCode = cond.getInt("code");
        String condTxt = cond.getString("txt");
        int fl = now.getInt("fl");
        int hum = now.getInt("hum");
        String pcpn = now.getString("pcpn");
        // ......            
    }
}

更详细的可以参考How to parse JSON in Android - StackOverFlow

整理后的JSON文件:

{"HeWeather data service 3.0":
    [
        {
            "basic":{
                "city":"漯河",
                "cnty":"中国",
                "id":"CN101181501",
                "lat":"33.563000",
                "lon":"114.026000",
                "update":{
                    "loc":"2015-09-15 14:46",
                    "utc":"2015-09-15 06:46"}
            },
            "daily_forecast":[{"astro":{"sr":"06:06","ss":"18:31"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-15","hum":"39","pcpn":"0.1","pop":"1","pres":"1016","tmp":{"max":"28","min":"13"},"vis":"10","wind":{"deg":"62","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:07","ss":"18:30"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-16","hum":"42","pcpn":"0.0","pop":"0","pres":"1016","tmp":{"max":"28","min":"16"},"vis":"10","wind":{"deg":"74","dir":"东北风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:28"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-17","hum":"41","pcpn":"0.0","pop":"0","pres":"1017","tmp":{"max":"27","min":"17"},"vis":"10","wind":{"deg":"137","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"06:08","ss":"18:27"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2015-09-18","hum":"36","pcpn":"0.0","pop":"1","pres":"1015","tmp":{"max":"28","min":"17"},"vis":"10","wind":{"deg":"117","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:09","ss":"18:26"},"cond":{"code_d":"101","code_n":"100","txt_d":"多云","txt_n":"晴"},"date":"2015-09-19","hum":"35","pcpn":"0.0","pop":"0","pres":"1014","tmp":{"max":"29","min":"17"},"vis":"10","wind":{"deg":"67","dir":"东北风","sc":"3-4","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:24"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-20","hum":"31","pcpn":"0.0","pop":"0","pres":"1011","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"121","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:10","ss":"18:23"},"cond":{"code_d":"100","code_n":"100","txt_d":"晴","txt_n":"晴"},"date":"2015-09-21","hum":"30","pcpn":"0.0","pop":"0","pres":"1010","tmp":{"max":"30","min":"17"},"vis":"10","wind":{"deg":"157","dir":"无持续风向","sc":"微风","spd":"0"}}],
            
            "hourly_forecast":[{"date":"2015-09-15 16:00","hum":"39","pop":"0","pres":"1015","tmp":"30","wind":{"deg":"60","dir":"东北风","sc":"微风","spd":"12"}},{"date":"2015-09-15 19:00","hum":"53","pop":"0","pres":"1015","tmp":"27","wind":{"deg":"68","dir":"东北风","sc":"微风","spd":"14"}},{"date":"2015-09-15 22:00","hum":"67","pop":"0","pres":"1017","tmp":"23","wind":{"deg":"72","dir":"东北风","sc":"微风","spd":"11"}}],
            
            "now":{
                "cond":{
                    "code":"100",
                    "txt":"晴"
                },
                "fl":"26",
                "hum":"38",
                "pcpn":"?",
                "pres":"1018",
                "tmp":"27",
                "vis":"10",
                "wind":{
                    "deg":"62",
                    "dir":"东北风",
                    "sc":"4-5",
                    "spd":"0"
                }
            },
            
            "status":"ok",
            "suggestion":{
                "comf":{"brf":"较舒适","txt":"白天天气晴好,您在这种天气条件下,会感觉早晚凉爽、舒适,午后偏热。"},
                "cw":{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},
                "drsg":{"brf":"热","txt":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"},
                "flu":{"brf":"少发","txt":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"},
                "sport":{"brf":"较适宜","txt":"天气较好,但考虑气温较低,推荐您进行室内运动,若户外适当增减衣物并注意防晒。"},
                "trav":{"brf":"适宜","txt":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},
                "uv":{"brf":"强","txt":"紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。"}
            }
        }
    ]
}

可以看看郭霖的第一行代码,里面有关于JSON解析的例子

新手上路,请多包涵
推荐问题
宣传栏