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

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

分析代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// TODO 状态处理 500 200
int res = ;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
    /**
     * 当返回码为200时,做处理
     * 得到服务器端返回json数据,并做处理
     * */
    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");
带数组形式的:
服务器端返回的数据格式为:
 
{"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 状态处理 500 200
int res = ;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
    /**
     * 当返回码为200时,做处理
     * 得到服务器端返回json数据,并做处理
     * */
    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());
    /**
     * 这里需要分析服务器回传的json格式数据,
     */
    JSONObject jsonObject = new JSONObject(builder.toString())
        .getJSONObject("calendar");
    JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
    for(int i=;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);
    }

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

[转]android中解析后台返回的json字符串的更多相关文章

  1. JavaScript怎么解析后台传入的json字符串

    var data = "{'name': '张三', 'age': 23, 'gender': true}"; //json字符串 var jso = JSON.parse(dat ...

  2. js的eval函数解析后台返回的json数据时为什加上圆括号eval("("+data+")"),而HTML页面定义的数据不用

    一,情况如下,这是成功代码: $(function () { $.ajax({ url: "Demo.aspx", type: "post", data: { ...

  3. ajax请求(二),后台返回的JSon字符串的转换

    ajax请求,json的转换 $.ajax({ url : "../folder/isExistAddFolder.do?t="+new Date(), type : 'POST' ...

  4. [thinkphp] 如何解析ajaxReturn返回的json字符串

    success: function(data){ var dataObj=eval("("+data+")");//转换为json对象 alert(dataOb ...

  5. Android中解析JSON形式的数据

    1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...

  6. 后台返回的json数据传到前端页面并在页面的表格中填充

    web前端页面: <table id="myTable" cellpadding="1" cellspacing="0" border ...

  7. Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表

    前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...

  8. android中解析文件的三种方式

    android中解析文件的三种方式     好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...

  9. jQuery中使用$.each()遍历后台响应的json字符串问题

    今天在做练习项目的时候,使用$.each()方法遍历后台传过来的json字符串时,chrome浏览器中发现如下问题  Cannot use 'in' operator to search for 'l ...

随机推荐

  1. Internet History, Technology and Security (Week1)

    Week1. History: Dawn of Electronic Computing War Time Computing and Conmmunication Keywords: Electro ...

  2. hdu_3804_Query on a tree(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3804 题意:给你一棵树,然后给出树上边的价值,然后给出x,y,问从1到x的边上不超过y的最大值为多少 ...

  3. BZOJ3036绿豆蛙的归宿

    BZOJ3036绿豆蛙的归宿 锲下陟凝 褰宓万 郝瓦痕膳 叶诙摞 А知π剧 椐猊∫距 屠缲佗 ゲ蕖揪 俜欧彖鹤 磲砩ほ #琛扶 觅电闸ス 捆鳢げ 浜窠 魂睨"烁 蕞滗浼 洒ヂ跪 ...

  4. 笨方法学python--多行,转义序列

    1 输入多行字符串的方法有2个,一个是使用换行符 \n.另一个是使用 "三引号". 2 针对不同的符号,有很多这样的"转义序列"(escape sequence ...

  5. ubuntu 安装LNMP

    How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04 PostedJune 13, 2012 802.8kviews ...

  6. API CLOUD 快捷键

    常用快捷键有:Ctrl+Z:撤销Ctrl+N:创建项目或文件Ctrl+Shift+F:代码格式化(这个经常用,可以美化代码,也可以通过这个检查代码是否出错)Ctrl+/ :注释和反注释Alt+/:强制 ...

  7. VMI

    在虚拟机外部监控虚拟机内部运行状态的方法被称为虚拟机自省(Virtual Machine Introspection,VMI).VMI允许特权域查看非特权域的运行状态,并能获得被监控虚拟机运行状况相关 ...

  8. UVALive 7291 Kinfolk(最近公共祖先)

    题目中的描述就很最近公共祖先,再说其实这个题并不难,就是麻烦点(代码其实可以化简的),我写的判定比较多. 方法:求出两者的最近公共祖先lca,在求出两者到lca的距离 分析:给出a和b,如果LCA(a ...

  9. Lightoj 1004 - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121396#problem/F http://lightoj.com/volume_showproblem.ph ...

  10. 二分图最大匹配 Hopcroft-Karp算法模板

    #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> ...