普通形式的:
服务器端返回的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. python字符串中的中文处理

    python字符串中的字符串默认并非是unicode,如果在字符创中使用Unicode字符,如中文字符,必须要经过转换, 方式1: text = u"中文" 方式2: text = ...

  2. JavaScript在智能手机上的应用-使用手机GPS定位用户所在城市

    ---------------------------- <script type="text/javascript" language="javascript&q ...

  3. http?https?相对协议?

    1 1 1 将CDN 上所有链接的协议默认设置为“相对协议”,也就是链接以 // 开头,前面去掉了 http: 或 https: 字样, 这样做的好处是浏览器能够根据你的网站所采用的协议来自动加载 C ...

  4. GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

  5. JSP基本语法--实例演练

    基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...

  6. Android Service学习之IntentService 深入分析

          什么是IntentService? (本文转自http://blog.csdn.net/gaojie314/archive/2010/11/28/6040701.aspx) 官方的解释是: ...

  7. Asp.Net调用Office组件操作时的DCOM配置 (转)

    Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...

  8. 关于在MyEclipse中页面中文乱码的问题

    1.首先在Window>preferences>General>Workspace中改为UTF-8. 2.将项目的Properties>Resource改为UTF-8. 3.将 ...

  9. 使WiFi具有保存历史连接的功能

    在wpa_supplicant.conf里面添加这个功能 update_config=1 就能更新了,保存了历史的连接AP,不用再输入密码

  10. Linq分组,linq方法分组

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: 01.public class ...