日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓json的解析

發(fā)布時(shí)間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓json的解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文由PurpleSword(jzj1993)原創(chuàng),轉(zhuǎn)載請注明 原文網(wǎng)址 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(); ????}

方法二(使用谷歌開源項(xiàng)目Gson,需要gson.jar程序庫的支持)
按照json的嵌套格式定義類,其中各種變量名稱和json變量名相同(int,String等類型皆可支持),數(shù)組則使用List格式,然后用new?Gson().fromJson(json,?WeatherResult.class)進(jìn)行解析(注意用try…catch捕獲異常)。
添加gson.jar程序庫到Java或安卓工程的方法:jar文件放到項(xiàng)目下的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;?//?"周三(今天,?實(shí)時(shí):23℃)"?/?"周四" ????????????protected?String?weather;?//?"霾"?/?"多云轉(zhuǎn)陣雨"?/?"陰轉(zhuǎn)多云" ????????????protected?String?wind;?//?"微風(fēng)" ????????????protected?String?temperature;?//?"22?~?10℃" ????????} ????} }






總結(jié)

以上是生活随笔為你收集整理的安卓json的解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。