android 解析json数据格式
前面我有用到android发送json数据;这里我想总结一下我用到的解析json数据格式的方式
json数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;
普通形式的:
服务器端返回的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。
android 解析json数据格式相关推荐
- 老罗Android开发视频教程( android解析json数据 )4集集合
老罗Android开发视频教程( android解析json数据 )4集集合 老罗Android开发视频教程( android解析json数据 ) 第一集android解析json数据 http:// ...
- android json格式解析,android之解析json数据格式详解
1.JSON解析 (1).解析Object之一: 解析方法: 1 JSONObject demoJson =newJSONObject(jsonString); 2 String url = demo ...
- Android 解析JSON数据
第一种使用JSONObject解析JSON数据,这是Android中最基本的数据解析方式 fromJson("{'status':200," +"'message':'o ...
- C语言cJSON库的使用,解析json数据格式
对于c语言来说是没有字典这样的结构的,所以对于解析json格式的数据来说不是那么好解析,但是有些时候又会需要处理这样的数据格式,这里就有一个解析json的函数库可以给我们使用了.这个函数库就是cJSO ...
- cjson 对象是json数组型结构体_C语言cJSON库的使用,解析json数据格式
对于c语言来说是没有字典这样的结构的,所以对于解析json格式的数据来说不是那么好解析,但是有些时候又会需要处理这样的数据格式,这里就有一个解析json的函数库可以给我们使用了.这个函数库就是cJSO ...
- Android解析JSON,你真的需要三方库?
一般情况下,如果服务器返回 JSON 数据,而且你又是做 Android 的,那么你首先想到的可能是GSON,或是fastJson这样的框架.这些框架能够很方便和快速的让我们将 JSON 转换成本地对 ...
- android解析json异常处理,Android的JSON异常而解析
我试图分析我的Android应用程序中的JSON(进一步我将填补与内容列表视图),所以我做了这个类:Android的JSON异常而解析 public class MainActivity extend ...
- android的json解析方式,Android解析JSON方式
JSON数据格式的定义: JSON的全称是:JavaScript Object Notation,是一种轻量级的数据交换格式.它构建于两种结构: 1."名称/值" 对的集合(a c ...
- SqlServer解析XML,解析JSON数据格式
一:解析XML(SQL2005版本开始支持) declare @pro_xml varchar(max)='<reg><node><pro_id>3</pro ...
最新文章
- python SSL error: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) 解决方法
- java中为什么使用上转型和下转型
- TensorFlow 教程 --进阶指南--3.1总览
- sqlite mysql excel_SQlite数据库信息怎么转成excel文件?
- html5 session 缓存,关于HTML5中的sessionStorage的会话级缓存使用
- IDEA安装MySQL版本以及驱动jar包下载问题
- 滨州智能dcs系统推荐_dcs系统厂家推荐
- ios下拉效果滑动滚出页面
- veu2x 消息订阅与发送
- Python使用for实现无限循环的多种方法
- thinkpadt410接口介绍_独家:ThinkPad T410与T400之细节对比
- 微信小程序学习(三):在微信开发者工具中,使用WeUI前端美化框架,微信小程序
- shell脚本编写创建多层目录,判断目录是否存在,存在则删除并且给文件赋予权限
- 上海最适合拍照的旅游地点
- MacOs “无法打开***,因为无法验证开发者...”
- 颠狂柳絮随风舞-_-!
- 分割(计数板)展示数字样式
- previous definition was here
- 美国计算机名校例如MIT ,CMU等招牌经典公开课程
- domain adaptation 小综述