【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2399|回复: 0

(转) android中用jsonObject解析json数据

[复制链接]
发表于 2013-1-22 17:14:49 | 显示全部楼层 |阅读模式
json数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;

普通形式的:
服务器端返回的json数据格式如下:
  1. {"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
复制代码
分析代码如下:
  1. // TODO 状态处理 500 200
  2.                 int res = 0;
  3.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
  4.                 if (res == 200) {
  5.                     /*
  6.                      * 当返回码为200时,做处理
  7.                      * 得到服务器端返回json数据,并做处理
  8.                      * */
  9.                     HttpResponse httpResponse = httpClient.execute(httpPost);
  10.                     StringBuilder builder = new StringBuilder();
  11.                     BufferedReader bufferedReader2 = new BufferedReader(
  12.                             new InputStreamReader(httpResponse.getEntity().getContent()));
  13.                     String str2 = "";
  14.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
  15.                             .readLine()) {
  16.                         builder.append(s);
  17.                     }
  18.                     Log.i("cat", ">>>>>>" + builder.toString());

  19. JSONObject jsonObject = new JSONObject(builder.toString())
  20.                         .getJSONObject("userbean");

  21.                 String Uid;
  22.                 String Showname;
  23.                 String Avtar;
  24.                 String State;

  25.                 Uid = jsonObject.getString("Uid");
  26.                 Showname = jsonObject.getString("Showname");
  27.                 Avtar = jsonObject.getString("Avtar");
  28.                 State = jsonObject.getString("State");
复制代码
带数组形式的:
服务器端返回的数据格式为:
  1. {"calendar":
  2.     {"calendarlist":
  3.             [
  4.             {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
  5.             {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
  6.             ]
  7.     }
  8. }
复制代码
分析代码如下:
  1. // TODO 状态处理 500 200
  2.                 int res = 0;
  3.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
  4.                 if (res == 200) {
  5.                     /*
  6.                      * 当返回码为200时,做处理
  7.                      * 得到服务器端返回json数据,并做处理
  8.                      * */
  9.                     HttpResponse httpResponse = httpClient.execute(httpPost);
  10.                     StringBuilder builder = new StringBuilder();
  11.                     BufferedReader bufferedReader2 = new BufferedReader(
  12.                             new InputStreamReader(httpResponse.getEntity().getContent()));
  13.                     String str2 = "";
  14.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
  15.                             .readLine()) {
  16.                         builder.append(s);
  17.                     }
  18.                     Log.i("cat", ">>>>>>" + builder.toString());
  19.                     /**
  20.                      * 这里需要分析服务器回传的json格式数据,
  21.                      */
  22.                     JSONObject jsonObject = new JSONObject(builder.toString())
  23.                             .getJSONObject("calendar");
  24.                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
  25.                     for(int i=0;i<jsonArray.length();i++){
  26.                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
  27.                         CalendarInfo calendarInfo = new CalendarInfo();
  28.                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
  29.                         calendarInfo.setTitle(jsonObject2.getString("title"));
  30.                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
  31.                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));
  32.                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
  33.                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
  34.                         calendarInfos.add(calendarInfo);
  35.                     }
复制代码
总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-5-6 15:04

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表