Android Json在线解析

导读:json数据格式解析我自己分为两种;一种是普通的,一种是带有数组形式的;普通形式的:服务器端返回的json数据格式如下:

        json数据格式解析我自己分为两种;

        一种是普通的,一种是带有数组形式的;

        普通形式的:
        服务器端返回的json数据格式如下:

1 {"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
2  
3   
         分析代码如下:
01 // TODO 状态处理 500 200
02                 int res = 0;
03                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
04                 if (res == 200) {
05                     /*
06                      * 当返回码为200时,做处理
07                      * 得到服务器端返回json数据,并做处理
08                      * */
09                     HttpResponse httpResponse = httpClient.execute(httpPost);
10                     StringBuilder builder = new StringBuilder();
11                     BufferedReader bufferedReader2 = new BufferedReader(
12                             newInputStreamReader(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 JSONObject jsonObject = new JSONObject(builder.toString())
21                         .getJSONObject("userbean");
22  
23                 String Uid;
24                 String Showname;
25                 String Avtar;
26                 String State;
27  
28                 Uid = jsonObject.getString("Uid");
29                 Showname = jsonObject.getString("Showname");
30                 Avtar = jsonObject.getString("Avtar");
31                 State = jsonObject.getString("State");
32   
          带数组形式的:         服务器端返回的数据格式为:  
01 {"calendar":
02     {"calendarlist":
03             [
04             {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
05             {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
06             ]
07     }
08 }
09  
10  }
11  
12 }
           分析代码如下:
01 // TODO 状态处理 500 200
02                 int res = 0;
03                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
04                 if (res == 200) {
05                     /*
06                      * 当返回码为200时,做处理
07                      * 得到服务器端返回json数据,并做处理
08                      * */
09                     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 = newJSONObject(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                     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值