博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 解析json数据格式
阅读量:6827 次
发布时间:2019-06-26

本文共 2978 字,大约阅读时间需要 9 分钟。

json数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;
 
普通形式的:
服务器端返回的json数据格式如下:
{
"
userbean
"
:{
"
Uid
"
:
"
100196
"
,
"
Showname
"
:
"
\疯\狂\的\猴\子
"
,
"
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
"
:
"
(\亲\子)ddssd
"
,
"
category_name
"
:
"
\默\认\分\类
"
,
"
showtime
"
:
"
1288927800
"
,
"
endshowtime
"
:
"
1288931400
"
,
"
allDay
"
:
false
}, 
            {
"
calendar_id
"
:
"
1706
"
,
"
title
"
:
"
(\旅\行)
"
,
"
category_name
"
:
"
\默\认\分\类
"
,
"
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。

 

 

转载于:https://www.cnblogs.com/qingblog/archive/2012/05/31/2528682.html

你可能感兴趣的文章
angularjs中ng-repeat-start与ng-repeat-end用法实例
查看>>
linux centos6.5 安装gcc-c++时出现 yum install gcc-c++ cannot find a valid baseurl for repo...
查看>>
Jsp动态生成表格
查看>>
MongoDB环境配置
查看>>
5_4 calvc
查看>>
Educational Codeforces Round 36 (Rated for Div. 2)
查看>>
深入理解javascript原型和闭包——从【自由变量】到【作用域链】
查看>>
java多线程
查看>>
Codevs1029 遍历问题
查看>>
远程连接提示“为Administrator连接到现存会话发生错误(Id 0).操作成功”
查看>>
nginx配置ssl证书
查看>>
fastjson SerializerFeature详解
查看>>
spring源码读书笔记
查看>>
HDOJ-1015 Safecracker 【DFS】
查看>>
读书笔记-->Java经典编程300例--明日科技--清华大学出版社(第一版)
查看>>
如何在存储过程中自动添加分区
查看>>
[并查集] POJ 1611 The Suspects
查看>>
C#设计模式总结
查看>>
团队开发------第一次冲刺第4天
查看>>
R对term进行层次聚类完整实例(tm包)
查看>>