Android之Json解析

JSON返回示例:
{ /*JSONObject*/
    "resultcode": "200",
    "reason": "SUCCESSED!",
    "result": [ /*JSONArray*/
        { /*JSONObject*/
            "city": "蘇州",  /*城市*/
            "PM2.5": "73",  /*PM2.5指數*/
            "AQI": "98",    /*空氣質量指數*/
            "quality": "良", /*空氣質量*/
            "PM10": "50",/*PM10*/
            "CO": "0.79",  /*一氧化碳*/
            "NO2": "65",  /*二氧化氮*/
            "O3": "28",    /*臭氧*/
            "SO2": "41",  /*二氧化硫*/
            "time": "2014-12-26 11:48:40"/*更新時間*/  
        }
    ],
    "error_code": 0
}

實例:JSONDemo
運行效果:


主要代碼片段:

protected void onPostExecute(String result) {  
        if (result != null) {  
            try {  
                JSONObject jsonObject = new JSONObject(result);  
                int resultCode = jsonObject.getInt("resultcode");  
                if (resultCode == 200) {  
                    JSONArray resultJsonArray = jsonObject.getJSONArray("result");  
                    JSONObject resultJsonObject = resultJsonArray.getJSONObject(0);  
                    String output = context.getString(R.string.city) + ": " + resultJsonObject.getString("city") + "\n"  
                            + context.getString(R.string.PM25) + ": " + resultJsonObject.getString("PM2.5") + "\n"  
                            + context.getString(R.string.AQI) + ": " + resultJsonObject.getString("AQI") + "\n"  
                            + context.getString(R.string.quality) + ": " + resultJsonObject.getString("quality") + "\n"  
                            + context.getString(R.string.PM10) + ": " + resultJsonObject.getString("PM10") + "\n"  
                            + context.getString(R.string.CO) + ": " + resultJsonObject.getString("CO") + "\n"  
                            + context.getString(R.string.NO2) + ": " + resultJsonObject.getString("NO2") + "\n"  
                            + context.getString(R.string.O3) + ": " + resultJsonObject.getString("O3") + "\n"  
                            + context.getString(R.string.SO2) + ": " + resultJsonObject.getString("SO2") + "\n"  
                            + context.getString(R.string.time) + ": " + resultJsonObject.getString("time") + "\n";  
                    tv_result.setText(output);  
                } else if (resultCode == 202) {  
                    String reason = jsonObject.getString("reason");  
                    tv_result.setText(reason);  
                } else {  
                    Toast.makeText(context, "查詢失敗",  
                            Toast.LENGTH_LONG).show();  
                    tv_result.setText("");  
                }  
            } catch (JSONException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        } else {  
            Toast.makeText(context, "查詢失敗",  
                                    Toast.LENGTH_LONG).show();  
            tv_result.setText("");  
        }  
    }    


分析:key值 'result' 後面對應的是數組,所以上面代碼用法是getJSONArray; 數組當中又是一個{ }對象,所以代碼中就是 resultJsonArray.getJSONObject(0);  這句 。 總而言之記住一個原則,value是什麼類型,那麼就用什麼類型去獲取。
發佈了72 篇原創文章 · 獲贊 51 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章