两种JSON数据类型的解析
son数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;
普通形式的:
服务器端返回的json数据格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
分析代码如下:
// TODO 状态处理 500 200
int res = 0;
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 = 0;
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=0;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。
转:http://www.cnblogs.com/tt_mc/archive/2011/01/04/1925327.html
两种JSON数据类型的解析的更多相关文章
- 两种设计模式和XML解析
两种设计模式 1.单例模式 模式的保证步骤:单例(是说在一个类中只能有一个对象)三条件 1.1类构造设置私有 private Play() { } 1.2 定义一个私有的静态的 类类型 变量 ...
- lua中,两种json和table互转方法的效率比较
lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...
- RabbitMQ Consumer获取消息的两种方式(poll,subscribe)解析
以下转自:http://blog.csdn.net/yangbutao/article/details/10395599 rabbitMQ中consumer通过建立到queue的连接,创建channe ...
- FTP协议的两种工作模式简单解析!
转载自百度百科:http://baike.baidu.com/link?url=KaBZmDM4IZ2v56MyoOnpjqKr0gADv_BRbgjlscYdyvh3-zDwINOHNPSi9Jlp ...
- net.sf.json和com.alibaba.fastjson两种json加工类的相关使用方法
com.alibaba.fastjson Fastjson是一个Java语言编写的高性能功能完善的JSON库.它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Jav ...
- ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较
JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...
- Json和XML解析
NSXMLParse 关于XML,有两种解析方式,分别是SAX(Simple API for XML,基于事件驱动的解析方式,逐行解析数据,采用协议回调机制)和DOM(Document Object ...
- JSON 解析的两种方法
今天帮朋友看了下JSON解析结果············· eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. ...
- Android中使用Gson解析JSON数据的两种方法
Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...
随机推荐
- bzoj3165 1568
1568是3165的弱化版,发的代码是3165的这道题完全没想出来,是看wyl大神的题解http://hi.baidu.com/wyl8899/item/2deafd3a376ef2d46d15e99 ...
- BZOJ1334: [Baltic2008]Elect
1334: [Baltic2008]Elect Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 386 Solved: 201[Submit][Sta ...
- Android学习之路——简易版微信为例(二)
1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...
- SharePoint Site Pages & Application Pages
转:http://www.wcode.net/plus/view.php?aid=1582071 SharePoint一个很重要的概念就是Site Pages和Application Pages.接触 ...
- [转]NHibernate之旅(11):探索多对多关系及其关联查询
本节内容 多对多关系引入 多对多映射关系 多对多关联查询 1.原生SQL关联查询 2.HQL关联查询 3.Criteria API关联查询 结语 多对多关系引入 让我们再次回顾在第二篇中建立的数据模型 ...
- [Buffalo] 一些SQL函数
取得当前时间的函数:GETDATE() 计算时间的函数:DATEADD(datepart,number,date) 计算两个时间差额:DATEDIFF(datepart,startdate,endda ...
- 【原】spark-submit提交应用程序的内部流程
我们经常通过spark-submit来提交spark应用程序,那么让我们一起看一下这里面到底发生了什么吧. 知识点: 1.CLI命令行界面启动Spark应用程序 Unix有两种方式:1)spark-s ...
- 非递归实现先序遍历 java leecode 提交
写完才知道自己学习都是似是而非啊,大家可以也在leecode上提交代码,纯手写,离开eclipse第一种方式:数据结构书上的,使用栈大概思路.1.不断将根节点的左孩子的左孩子直到为空,在这个过程入栈. ...
- HW3.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 怎么加 一个 hyperlink 到 e-mail template for CRM
Recently I had a client inquire as to how one would insert a hyperlink into a CRM email template. Wh ...