Android之Json-@July

来源:互联网 发布:mac apache php 403 编辑:程序博客网 时间:2024/05/20 13:19
json数据格式解析我自己分为两种;

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

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

{
"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

分析代码如下:
[java] view plaincopy
  1. // TODO 状态处理 500 200   
  2.   
  3.                 final HttpPost httpPost=new HttpPost(url);    
  4.                 int res = 0;   
  5.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
  6.                 if (res == 200) {   
  7.                     /*  
  8.                      * 当返回码为200时,做处理  
  9.                      * 得到服务器端返回json数据,并做处理  
  10.                      * */   
  11.                     HttpResponse httpResponse = httpClient.execute(httpPost);  
  12.     //构建可变字符串 
  13.                     StringBuilder builder = new StringBuilder();   
  14.     //响应的实体数据 缓冲处理流里面包了一个对象是处理流
  15.                     BufferedReader bufferedReader2 = new BufferedReader(   
  16.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
  17.                     String str2 = "";   
  18.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
  19.                             .readLine()) {   
  20.                         builder.append(s);   
  21.                     }   
  22.                     Log.i("cat"">>>>>>" + builder.toString());  
  23.   
  24.                 JSONObject jsonObject = new JSONObject(builder.toString()).getJSONObject("userbean");   
  25.   
  26.                 String Uid;   
  27.                 String Showname;   
  28.                 String Avtar;   
  29.                 String State;   
  30.   
  31.                 Uid = jsonObject.getString("Uid");   
  32.                 Showname = jsonObject.getString("Showname");   
  33.                 Avtar = jsonObject.getString("Avtar");   
  34.                 State = jsonObject.getString("State");  


带数组形式的:
服务器端返回的数据格式为:

{
"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
            ] 
    } 
}

分析代码如下:
[java] view plaincopy
  1. // TODO 状态处理 500 200   
  2.                 final HttpPost httpPost=new HttpPost(url);    
  3.                 int res = 0;   
  4.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
  5.                 if (res == 200) {   
  6.                     /*  
  7.                      * 当返回码为200时,做处理  
  8.                      * 得到服务器端返回json数据,并做处理  
  9.                      * */   
  10.                     HttpResponse httpResponse = httpClient.execute(httpPost);   
  11.                     StringBuilder builder = new StringBuilder();   
  12.                     BufferedReader bufferedReader2 = new BufferedReader(   
  13.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
  14.                     String str2 = "";   
  15.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
  16.                             .readLine()) {   
  17.                         builder.append(s);   
  18.                     }   
  19.                     Log.i("cat"">>>>>>" + builder.toString());   
  20.                     /**  
  21.                      * 这里需要分析服务器回传的json格式数据,  
  22.                      */   
  23.                     JSONObject jsonObject = new JSONObject(builder.toString())   
  24.                             .getJSONObject("calendar");   
  25.                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");   
  26.                     for(int i=0;i<jsonArray.length();i++){   
  27.                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);   
  28.                         CalendarInfo calendarInfo = new CalendarInfo();   
  29.                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));   
  30.                         calendarInfo.setTitle(jsonObject2.getString("title"));   
  31.                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));   
  32.                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));   
  33.                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));   
  34.                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));   
  35.                         calendarInfos.add(calendarInfo);   
  36.                     }  

总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。
0 0