zoukankan      html  css  js  c++  java
  • json数据解析

     1 json数据格式解析我自己分为两种;
     2 
     3 一种是普通的,一种是带有数组形式的;
     4 
     5 
     6  
     7 
     8 普通形式的:
     9 服务器端返回的json数据格式如下:
    10 
    11 {"userbean":{"Uid":"100196","Showname":"u75afu72c2u7684u7334u5b50","Avtar":null,"State":1}}
    12 
    13 分析代码如下:
    14 
    15 // TODO 状态处理 500 200 
    16                 int res = 0; 
    17                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); 
    18                 if (res == 200) { 
    19                     /* 
    20                      * 当返回码为200时,做处理 
    21                      * 得到服务器端返回json数据,并做处理 
    22                      * */ 
    23                     HttpResponse httpResponse = httpClient.execute(httpPost); 
    24                     StringBuilder builder = new StringBuilder(); 
    25                     BufferedReader bufferedReader2 = new BufferedReader( 
    26                             new InputStreamReader(httpResponse.getEntity().getContent())); 
    27                     String str2 = ""; 
    28                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2 
    29                             .readLine()) { 
    30                         builder.append(s); 
    31                     } 
    32                     Log.i("cat", ">>>>>>" + builder.toString());
    33 
    34 JSONObject jsonObject = new JSONObject(builder.toString()) 
    35                         .getJSONObject("userbean"); 
    36 
    37                 String Uid; 
    38                 String Showname; 
    39                 String Avtar; 
    40                 String State; 
    41 
    42                 Uid = jsonObject.getString("Uid"); 
    43                 Showname = jsonObject.getString("Showname"); 
    44                 Avtar = jsonObject.getString("Avtar"); 
    45                 State = jsonObject.getString("State");
    46 带数组形式的:
    47 服务器端返回的数据格式为:
    48 
    49 {"calendar": 
    50     {"calendarlist": 
    51             [ 
    52             {"calendar_id":"1705","title":"(u4eb2u5b50)ddssd","category_name":"u9ed8u8ba4u5206u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
    53             {"calendar_id":"1706","title":"(u65c5u884c)","category_name":"u9ed8u8ba4u5206u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
    54             ] 
    55     } 
    56 }
    57 
    58 分析代码如下:
    59 
    60 // TODO 状态处理 500 200 
    61                 int res = 0; 
    62                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); 
    63                 if (res == 200) { 
    64                     /* 
    65                      * 当返回码为200时,做处理 
    66                      * 得到服务器端返回json数据,并做处理 
    67                      * */ 
    68                     HttpResponse httpResponse = httpClient.execute(httpPost); 
    69                     StringBuilder builder = new StringBuilder(); 
    70                     BufferedReader bufferedReader2 = new BufferedReader( 
    71                             new InputStreamReader(httpResponse.getEntity().getContent())); 
    72                     String str2 = ""; 
    73                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2 
    74                             .readLine()) { 
    75                         builder.append(s); 
    76                     } 
    77                     Log.i("cat", ">>>>>>" + builder.toString()); 
    78                     /** 
    79                      * 这里需要分析服务器回传的json格式数据, 
    80                      */ 
    81                     JSONObject jsonObject = new JSONObject(builder.toString()) 
    82                             .getJSONObject("calendar"); 
    83                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist"); 
    84                     for(int i=0;i<jsonArray.length();i++){ 
    85                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); 
    86                         CalendarInfo calendarInfo = new CalendarInfo(); 
    87                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id")); 
    88                         calendarInfo.setTitle(jsonObject2.getString("title")); 
    89                         calendarInfo.setCategory_name(jsonObject2.getString("category_name")); 
    90                         calendarInfo.setShowtime(jsonObject2.getString("showtime")); 
    91                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime")); 
    92                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay")); 
    93                         calendarInfos.add(calendarInfo); 
    94                     }
    95 
    96 总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。
  • 相关阅读:
    数据结构和算法学习笔记七:图的搜索
    数据结构和算法学习笔记六:图的相关实现
    Unity常用的3D数学知识
    Unity计时器--简版
    基于前缀树的红点系统
    数据结构和算法学习笔记五:图的基本概念
    C内存管理
    如何解决KEIL中文乱码问题
    C语言结构体变量作为函数形参
    C的结构体
  • 原文地址:https://www.cnblogs.com/xmb7/p/3190782.html
Copyright © 2011-2022 走看看