Android json數(shù)據(jù)解析詳解及實例代碼
Android json數(shù)據(jù)解析詳解
移動開發(fā)經(jīng)常要與服務(wù)器數(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ù)格式是一個鍵對應(yīng)一個值。你可以先打印出原始數(shù)據(jù)strResult,就知道jsonObj.getString("sportsTitle");這雙引號里面鍵是什么。
2.數(shù)組形式j(luò)son數(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解析天氣預(yù)報
- 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中g(shù)son、jsonobject解析JSON的方法詳解
- Android解析json數(shù)組對象的方法及Apply和數(shù)組的三個技巧
- Android隨手筆記44之JSON數(shù)據(jù)解析
- Android學(xué)習(xí)筆記45之gson解析json
- android原生JSON解析實例
相關(guān)文章
Android編程之SurfaceView學(xué)習(xí)示例詳解
這篇文章主要介紹了Android編程之SurfaceView學(xué)習(xí)示例,結(jié)合實例分析了SurfaceView的功能、使用方法與注意事項,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10android studio按鈕監(jiān)聽的5種方法實例詳解
這篇文章主要介紹了android studio按鈕監(jiān)聽的5種方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android中的webview支持頁面中的文件上傳實例代碼
本篇文章主要介紹了Android中的webview支持頁面中的文件上傳,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android ActivityManagerService啟動流程詳解
這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務(wù),運行在system_server進程。此服務(wù)十分重要,因為它管理著安卓的四大組件,是安卓APP開發(fā)者最常接觸到的一個服務(wù)2023-02-02Android自定義HorizontalScrollView實現(xiàn)qq側(cè)滑菜單
本文主要介紹了android自定義HorizontalScrollView實現(xiàn)qq側(cè)滑菜單的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04