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。
from :http://blog.csdn.net/cwx01perfect/article/details/7559832
相关推荐
此资源包含服务端使用的JSONObject的jar包以及服务端和客户端的代码
有个哥们10分要,我就看不惯了,2分要的拿去,本来想免费的结果最少要2分不好意思了各位
C#的一个解析JSON的工具类,像java一样优雅的解析JSON数据,包含有JSONObject和JSONArray对象。 JSONObject json = new JSONObject(); json.Put("sex", "男"); json.Put("age", 123); json.Put("name", "张三"); ...
java 解析 json文件java 解析 json文件java 解析 json文件java 解析 json文件java 解析 json文件
1.解析json字符串 2.将json字符串转换为java对象 3.将java对象转换为json字符串
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换...
主要介绍了Java使用JSONObject操作json,结合实例形式较为详细的分析了Java使用JSONObject解析json数据相关原理、使用技巧与操作注意事项,需要的朋友可以参考下
教你如何连接数据库解析Kettle中的json串
简封Volley、Gson和JsonObject生成Json数据、添加Volley请求头体Json参数、Gson解析返回的Json
用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject等插件封装的JSON对象,与此亦是大同小异,这里...
下面小编就为大家带来一篇使用JSONObject生成和解析json的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
此压缩包包含两个jar:json-lib-2.4-jdk15.jar和ezmorph-1.0.6.jar 这两个包都是java解析json数据的依赖包,而且是最新版,开源的东东好多人还要分,俺不要分,只希望对你有用。
这个工程里面包含了解析json的jar,和解析json的一个小例子。 在运行这个工程时,需要重新,自行导入工程中,lib目录下的jar包 JSONObject jsonObject = new JSONObject(jsonString);//将字符串转换成JSON对象 ...
使用迭代器以及gson和jsonobject解析json数据
方便好用的json解析库,每个json元素都是一个JsonElement对象,通过这个对象,你可以很方便的从Json子类对象中得到你想要的值,不需要你进一步转换,用法如: JsonObject jsonObject = new JsonObject (...); 在...
解析Json字符串直接转换为对象(包括多层List集合 嵌套)所用jar包
java把string转换为json,之后解析需要用到的jar包和依赖包。
在用java写爬虫的时候需要用jsonobject解析json数据,需要导入多个包
解析json的工具类,包括JSONObject、JSONArray、JSONException、JSONString等