普通形式的:
服务器端返回的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. Windows 8/7下还原系统默认扩展名打开方式类型

    在百度知道上如果你搜“改回选错的打开方式”,看到的大多数都是XP系统的方法,不管是批处理还是别的方法,但适用于Windows 8/7的只有修改注册表的方法. 因为Windows 7你也就根本找不到[工 ...

  2. glusterfs——volume管理

    Q: 常用的命令有哪些? 创建volume: gluster volume create NAME stripe SCOUNT replica RCOUNT transport TYPE  BRICK ...

  3. List-----Array

    1.Definition Arry数组是一种连续储存的List 储存方式:将线性表中的元素一次储存在连续的储存空间中. Computer's logical structure: 逻辑位置上相邻的元素 ...

  4. ios 做的一个三个数求平均数 最大数 最小数

    #import "ViewController.h" @interface ViewController ()@property (weak, nonatomic) IBOutle ...

  5. swfupload在chrome中点击上传图片按钮无反应的解决办法

    chrome 22.0.XXXXX dev版上传图片按钮点击无反应原因:是GOOGLE的内建Flash PPAPI外挂所导致的. 问题原因: 由于Google浏览器(Chrome),在最新测试版22. ...

  6. Shell学习笔记 ——第一天

    1.程序第一行  指定执行Shell的程序 #!/bin/sh #!用来告诉系统它后面的参数是用来执行该文件的程序 2.在控制台输出信息 echo "Hello Shell" #! ...

  7. ubuntu 上下载PHP的源代码

    参考: https://vpsineu.com/blog/how-to-build-and-install-php-5-6-9-from-source-on-ubuntu-14-04-vps/ 直接 ...

  8. git简单常用的命令

    git status --查看文件状态 git add+文件路径 --上传到缓存区 git add --all --全部传到缓存区 git commit -m '描述' --对上传文件做描述 git ...

  9. C++对文件进行加密解密

    1. 起因: 需要对游戏资源进行加密 2. 解决方案: 通过网络查询,xxtea是一款轻量级的加密工具,使用简单方便 3. 加密解密 xxtea只有两个函数,加密:xxtea_encrypt 解密:x ...

  10. 获取Excel数据(或部分数据)并导出成txt文本格式

    运行代码前先导入jxl架包,以下代码仅供参考: 测试excel文件(我要获取该excel的内容为省.县.乡.村.组和PH的值): ExcelTest01类代码如下: // 读取Excel的类 impo ...