Android json數(shù)據(jù)解析詳解及實例代碼
Android json數(shù)據(jù)解析詳解
移動開發(fā)經(jīng)常要與服務器數(shù)據(jù)交互,也常使用json數(shù)據(jù)格式,那就說說Android json解析。
1.最簡單json格式解析如下:
//解析json
ry {
JSONTokener jsonParser = new JSONTokener(strResult);
JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
String strsportsTitle = jsonObj.getString("sportsTitle");
int nid= jsonObj.getInt("id");
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
字符串strResult就是需要解析json數(shù)據(jù)了。用過json數(shù)據(jù)格式都知道,json數(shù)據(jù)格式是一個鍵對應一個值。你可以先打印出原始數(shù)據(jù)strResult,就知道jsonObj.getString("sportsTitle");這雙引號里面鍵是什么。
2.數(shù)組形式json數(shù)據(jù)解析如下:
try {
JSONArray jsonArray = new JSONArray(strResult);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.optJSONObject(i);
id[i] = jsonObj.getInt("id");
time[i] = jsonObj.getString("time");
users[i] = jsonObj.getString("users");
roomTitle[i] = jsonObj.getString("roomTitle");
}
} catch (JSONException e) {
System.out.println("Jsons parse error !");
e.printStackTrace();
}
3.json里面嵌套json數(shù)據(jù)解析如下:
try {
JSONArray jsonArray = new JSONArray(strResult);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.optJSONObject(i);
String strachievement = jsonObj.getString("achievement");
String strmember = jsonObj.getString("member");
try {
JSONTokener jsonParser1 = new JSONTokener(
achievement);
JSONObject jsonObj1 = (JSONObject) jsonParser1
.nextValue();
nametype[i] = jsonObj1.getString("name");
type[i] = jsonObj1.getString("type");
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
}
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
嵌套json數(shù)據(jù),其實都是一樣的。多解析一次而已。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android編程簡單解析JSON格式數(shù)據(jù)的方法示例
- Android使用OKHTTP解析JSON數(shù)據(jù)的實例代碼
- android JSON解析數(shù)據(jù) android解析天氣預報
- Android 中對JSON數(shù)據(jù)解析實例代碼
- Android解析json數(shù)據(jù)示例代碼(三種方式)
- Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進行解析的方法
- Android之解析JSON數(shù)據(jù)示例(android原生態(tài),F(xiàn)astJson,Gson)
- Android系列---JSON數(shù)據(jù)解析的實例
- Android利用Gson解析嵌套多層的Json的簡單方法
- Android M(6.x)使用OkHttp包解析和發(fā)送JSON請求的教程
- Android解析JSON數(shù)據(jù)的方法分析
- Android json解析及簡單例子
- android解析JSON數(shù)據(jù)
- Android中gson、jsonobject解析JSON的方法詳解
- Android解析json數(shù)組對象的方法及Apply和數(shù)組的三個技巧
- Android隨手筆記44之JSON數(shù)據(jù)解析
- Android學習筆記45之gson解析json
- android原生JSON解析實例
相關文章
Android audio音頻流數(shù)據(jù)異常問題解決分析
這篇文章主要為大家介紹了Android audio音頻流數(shù)據(jù)異常問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
android控件Banner實現(xiàn)簡單輪播圖效果
這篇文章主要為大家詳細介紹了android控件Banner實現(xiàn)簡單輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android學習筆記之ContentProvider和Uri詳解
本篇文章主要介紹了Android學習筆記之ContentProvider和Uri詳解,對于學習Android的朋友具有一定的參考價值,有需要可以可以了解一下。2016-11-11

