永发信息网

为什么返回的数据类型不是json

答案:1  悬赏:40  手机版
解决时间 2021-12-29 19:01
为什么返回的数据类型不是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) {

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) {

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("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。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
新买回来的衣服怎么洗才能不褪色?
怀德桥派出所地址在哪?我要去那里办事
单选题依次填入句中横线处的词语,最恰当的一
单片机usb接口下载程序时不能供电什么原因
局前街派出所地址有知道的么?有点事想过去!
《渔夫和他的妻子》这个童话有什么启示?
有类似极品影视的APP嘛
去香港帮别人代购物品犯法吗
住房公积金英语怎么说
祺达小车更换了刹车皮,油耗高了,为什么?
ghost windows server 2003 64位企业版 可以
常州市武进区公安局横林派出所地址在哪?我要
企业的人才流失该如何对待?
新建的厂房墙体在晃动属于危房吗
关于x的一元二次方程x2-kx+5(k-5)=0的两个
推荐资讯
单选题研究光对鼠妇生活的影响时,实验所用鼠
怎样在SolidWorks零件中插入其他零件
—How are things going?—The disabled no
昌吉州奇台县公安局交交警大队办公位置在哪啊
喜欢玉兰油的姐妹,能不能介绍一下效果
2012年安徽省粮食总产657.8亿斤,657.8亿这个
请问大师我是炉中火命做什么行业合适呢?我想
发达的近义词和反义词
起点的推荐票有什么用
王者荣耀怎么退出界面
如果把鱼从鱼塘里捞出来,不用多久就会死亡,
健康证有什么用处
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?