安卓json的解析
本文由PurpleSword(jzj1993)原創,轉載請注明 原文網址 http://blog.csdn.net/jzj1993
方法一(使用安卓包含的JSON解析類)
import?org.json.JSONArray; import?org.json.JSONObject; import?org.json.JSONTokener;
? ? try?{ ????????JSONTokener?tokener?=?new?JSONTokener(json); ????????JSONObject?joResult?=?new?JSONObject(tokener); ????????JSONArray?words?=?joResult.getJSONArray("ws"); ????????for?(int?i?=?0;?i?<?words.length();?i++)?{ ????????????JSONArray?items?=?words.getJSONObject(i).getJSONArray("cw"); ????????????JSONObject?obj?=?items.getJSONObject(0); ????????????chComTmp?=?outerparseCom(obj.getString("w")); ????????} ????}?catch?(Exception?e)?{ ????????e.printStackTrace(); ????}
方法二(使用谷歌開源項目Gson,需要gson.jar程序庫的支持)
按照json的嵌套格式定義類,其中各種變量名稱和json變量名相同(int,String等類型皆可支持),數組則使用List格式,然后用new?Gson().fromJson(json,?WeatherResult.class)進行解析(注意用try…catch捕獲異常)。
添加gson.jar程序庫到Java或安卓工程的方法:jar文件放到項目下的libs文件夾中,右擊jar文件,菜單中選擇BuildPath-->Add to BuildPath即可。
import?com.google.gson.Gson; import?com.google.gson.JsonSyntaxException;
public?class?WeatherResult?{ ????private?String?error;?//?0 ????private?String?status;?//?"success" ????private?String?date;?//?"2014-03-26" ????private?List<Results>?results; ????private?WeatherResult()?{ ????} ????public?static?WeatherResult?fromJson(String?json)?{ ????????try?{ ????????????return?new?Gson().fromJson(json,?WeatherResult.class); ????????}?catch?(JsonSyntaxException?e)?{ ????????????return?null; ????????} ????} ????public?class?Results?{ ????????protected?String?currentCity;?//?"北京" ????????protected?List<WeatherData>?weather_data; ????????public?class?WeatherData?{ ????????????protected?String?date;?//?"周三(今天,?實時:23℃)"?/?"周四" ????????????protected?String?weather;?//?"霾"?/?"多云轉陣雨"?/?"陰轉多云" ????????????protected?String?wind;?//?"微風" ????????????protected?String?temperature;?//?"22?~?10℃" ????????} ????} }
方法一(使用安卓包含的JSON解析類)
import?org.json.JSONArray; import?org.json.JSONObject; import?org.json.JSONTokener;
? ? try?{ ????????JSONTokener?tokener?=?new?JSONTokener(json); ????????JSONObject?joResult?=?new?JSONObject(tokener); ????????JSONArray?words?=?joResult.getJSONArray("ws"); ????????for?(int?i?=?0;?i?<?words.length();?i++)?{ ????????????JSONArray?items?=?words.getJSONObject(i).getJSONArray("cw"); ????????????JSONObject?obj?=?items.getJSONObject(0); ????????????chComTmp?=?outerparseCom(obj.getString("w")); ????????} ????}?catch?(Exception?e)?{ ????????e.printStackTrace(); ????}
方法二(使用谷歌開源項目Gson,需要gson.jar程序庫的支持)
按照json的嵌套格式定義類,其中各種變量名稱和json變量名相同(int,String等類型皆可支持),數組則使用List格式,然后用new?Gson().fromJson(json,?WeatherResult.class)進行解析(注意用try…catch捕獲異常)。
添加gson.jar程序庫到Java或安卓工程的方法:jar文件放到項目下的libs文件夾中,右擊jar文件,菜單中選擇BuildPath-->Add to BuildPath即可。
import?com.google.gson.Gson; import?com.google.gson.JsonSyntaxException;
public?class?WeatherResult?{ ????private?String?error;?//?0 ????private?String?status;?//?"success" ????private?String?date;?//?"2014-03-26" ????private?List<Results>?results; ????private?WeatherResult()?{ ????} ????public?static?WeatherResult?fromJson(String?json)?{ ????????try?{ ????????????return?new?Gson().fromJson(json,?WeatherResult.class); ????????}?catch?(JsonSyntaxException?e)?{ ????????????return?null; ????????} ????} ????public?class?Results?{ ????????protected?String?currentCity;?//?"北京" ????????protected?List<WeatherData>?weather_data; ????????public?class?WeatherData?{ ????????????protected?String?date;?//?"周三(今天,?實時:23℃)"?/?"周四" ????????????protected?String?weather;?//?"霾"?/?"多云轉陣雨"?/?"陰轉多云" ????????????protected?String?wind;?//?"微風" ????????????protected?String?temperature;?//?"22?~?10℃" ????????} ????} }
總結
- 上一篇: ffmpeg:视频提取mp3和flac转
- 下一篇: 安卓线程相关 HandlerThread