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。
分享到:
相关推荐
本着探究 JSON 原理的目的,我将会在这DEMO中实现了一个简单的JSON解析器。由于 JSON 本身比较简单,解析起来也并不复杂。所以如果大家感兴趣的话,在看完本DEMO后,不妨自己动手实现一个 JSON 解析器。
C#对多级json解析,非常实用,此代码写在load事件里面,然后可以了解解析json的原理。尤其是多级解析。
自己写的json解析,不使用第三方的jar包,简单易懂,分为本地json解析和online json解析,网上的只需要修改一下url地址即可
JSON 解析 http://blog.csdn.net/zxw136511485/article/details/51451523
易语言JSON解析模块2.0源码例程程序结合易语言扩展界面支持库和应用接口支持库,调用API函数实现JSON解析、生成、编辑。点评:易语言JSON解析模块2.0源码通过封装JSON类形成稳定强大的json处理核心。资源作者:。...
sql server 2014 JSON解析到表函数 CREATE FUNCTION [dbo].[parseJSON]( @JSON NVARCHAR(MAX)) RETURNS @hierarchy TABLE ( element_id INT IDENTITY(1, 1) NOT NULL, ...
易语言页面订单json解析源码,页面订单json解析,初始化表格,从网页源码中读取订单到数组,将订单数组内容写到表格
Unity能够在WebGL包使用的json解析.dll
c# json 解析+C# newtonsoft.json.dll 解析类库文件。
易语言、易Json解析模块、水淼Json 易语言水淼JSON解析模块源码自主解析JSON数据类型,使用树来直观清晰显示出所有结构,辅助程序员快速定位JSON节点提高开发效率。
Json解析,Gson解析,本地,网络解析简单的案例
最简单最高效的Json解析,快捷小巧方便简洁
delphi_json解析格式化源码 delphi_json解析格式化源码 delphi_json解析格式化源码
PB Json解析库
JSON解析最新版20151017超简JSON效验解析器
json解析的三种方式
C#中Json 解析类库,C# 解析json 时,通常使用的是强类型对象反序列化,但是有时候不知道具体的对象时,使用该类库,使用dictionary 解析弱类型数据
最全的 json解析JAR包 最全的 json解析JAR包 最全的 json解析JAR包
适用于json解析,美观直观的观看数据
网上有CJson来解析数据,但对于单片机来说还是比较繁杂。就自己写了个JSON解析程序,非常精简,只有一百多行。 当然肯定也有不少BUG,欢迎大家指证、优化。