android 解析json數(shù)據(jù)格式的方法
json數(shù)據(jù)格式解析我自己分為兩種;
一種是普通的,一種是帶有數(shù)組形式的;
普通形式的:
服務(wù)器端返回的json數(shù)據(jù)格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
分析代碼如下:
// TODO 狀態(tài)處理 500 200
int res = 0;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
/*
* 當(dāng)返回碼為200時(shí),做處理
* 得到服務(wù)器端返回json數(shù)據(jù),并做處理
* */
HttpResponse httpResponse = httpClient.execute(httpPost);
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String str2 = "";
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
builder.append(s);
}
Log.i("cat", ">>>>>>" + builder.toString());
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("userbean");
String Uid;
String Showname;
String Avtar;
String State;
Uid = jsonObject.getString("Uid");
Showname = jsonObject.getString("Showname");
Avtar = jsonObject.getString("Avtar");
State = jsonObject.getString("State");
帶數(shù)組形式的:
服務(wù)器端返回的數(shù)據(jù)格式為:
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
}
分析代碼如下:
// TODO 狀態(tài)處理 500 200
int res = 0;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
/*
* 當(dāng)返回碼為200時(shí),做處理
* 得到服務(wù)器端返回json數(shù)據(jù),并做處理
* */
HttpResponse httpResponse = httpClient.execute(httpPost);
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String str2 = "";
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
builder.append(s);
}
Log.i("cat", ">>>>>>" + builder.toString());
/**
* 這里需要分析服務(wù)器回傳的json格式數(shù)據(jù),
*/
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("calendar");
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
CalendarInfo calendarInfo = new CalendarInfo();
calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
calendarInfo.setTitle(jsonObject2.getString("title"));
calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
calendarInfo.setShowtime(jsonObject2.getString("showtime"));
calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
calendarInfos.add(calendarInfo);
}
總結(jié),普通形式的只需用JSONObject ,帶數(shù)組形式的需要使用JSONArray 將其變成一個(gè)list。
相關(guān)文章
實(shí)現(xiàn)Android studio設(shè)置自動(dòng)導(dǎo)包及自動(dòng)導(dǎo)包快捷鍵
這篇文章主要介紹了實(shí)現(xiàn)Android studio設(shè)置自動(dòng)導(dǎo)包及自動(dòng)導(dǎo)包快捷鍵的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android自定義View實(shí)現(xiàn)波浪動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)波浪動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫訪問實(shí)例
下面小編就為大家分享一篇Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫訪問實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
android網(wǎng)絡(luò)編程之a(chǎn)ndroid連接網(wǎng)絡(luò)的簡(jiǎn)單示例代碼
這篇文章主要介紹了android連接網(wǎng)絡(luò)的簡(jiǎn)單示例,需要的朋友可以參考下2014-04-04
Android實(shí)現(xiàn)美團(tuán)外賣底部導(dǎo)航欄動(dòng)畫
這篇文章主要介紹了Android實(shí)現(xiàn)美團(tuán)外賣底部導(dǎo)航欄動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法
ImageSwitcher是Android中控制圖片展示效果的一個(gè)控件。本文給大家介紹Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法,需要的朋友參考下吧2016-12-12
Android 仿抖音的評(píng)論列表的UI和效果的實(shí)現(xiàn)代碼
抖音是一款音樂創(chuàng)意短視頻社交軟件,此app已在android各大應(yīng)用商店和app store 上線。下面小編給大家?guī)砹薃ndroid 仿抖音的評(píng)論列表的UI和效果的實(shí)現(xiàn)代碼,感興趣的朋友參考下吧2018-03-03
android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除
本篇文章主要介紹了android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android xml布局及RotateAnimation動(dòng)畫類相關(guān)操作技巧,需要的朋友可以參考下2018-02-02

