[转]android中解析后台返回的json字符串
服务器端返回的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字符串的更多相关文章
- JavaScript怎么解析后台传入的json字符串
var data = "{'name': '张三', 'age': 23, 'gender': true}"; //json字符串 var jso = JSON.parse(dat ...
- js的eval函数解析后台返回的json数据时为什加上圆括号eval("("+data+")"),而HTML页面定义的数据不用
一,情况如下,这是成功代码: $(function () { $.ajax({ url: "Demo.aspx", type: "post", data: { ...
- ajax请求(二),后台返回的JSon字符串的转换
ajax请求,json的转换 $.ajax({ url : "../folder/isExistAddFolder.do?t="+new Date(), type : 'POST' ...
- [thinkphp] 如何解析ajaxReturn返回的json字符串
success: function(data){ var dataObj=eval("("+data+")");//转换为json对象 alert(dataOb ...
- Android中解析JSON形式的数据
1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...
- 后台返回的json数据传到前端页面并在页面的表格中填充
web前端页面: <table id="myTable" cellpadding="1" cellspacing="0" border ...
- Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表
前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- jQuery中使用$.each()遍历后台响应的json字符串问题
今天在做练习项目的时候,使用$.each()方法遍历后台传过来的json字符串时,chrome浏览器中发现如下问题 Cannot use 'in' operator to search for 'l ...
随机推荐
- 《Windows驱动开发技术详解》之Windows内存管理
虚拟内存地址 Windows所有的程序(Ring0和Ring3层)可以操作的都是虚拟内存.有一部分单元会和物理内存对应起来,但并非一一对应,多个虚拟内存页可以映射同一个物理内存页.还有一部分单元会被映 ...
- python 字典排序,列表排序详细
在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序.Python中字典的排序分为按“键”排序和按“值”排序. 1.按“值 ...
- python自动化测试
python自动化测试 欢迎光临 python自动化测试 小站! 小站基于IT行业,重点关注python,软件自动化测试,测试等. 联系方式 飞信 372818219 相关的群:python开发自动化 ...
- elasticsearch快照和恢复
摘要:es可以通过简单的命令对索引或者整个集群进行快照和恢复 快照和恢复 Snapshot and restore 模块允许创建单个索引或者整个集群的快照到远程仓库. 在初始版本里只支持共享文件系统的 ...
- LeetCode OJ 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- TextBox只读时不能通过后台赋值取值解决办法
给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 1.不设置ReadOnly,设置onfocus=this.blur( ...
- Floodfill算法——求独立子图个数
Floodfill--漫水填充法(也称种子填充法)简单来说就是求一个图中独立子图的个数并将其描述出不同的状态.Floodfill在计算机图形学有着非常广泛的运用,比如图像分割.物体识别之类.基于Flo ...
- HTML/CSS font-family对应的中英文名称 宋体 微软雅黑
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- mysql 常用命令集锦[绝对精华]
一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u roo ...
- eclipse配置tomcat及修改tomcat默认根目录
1.安装eclipse for j2ee和tomcat: 2.下载tomcat对eclipse的插件:http://www.eclipsetotale.com/tomcatPlugin.html 下载 ...