欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 解析json数据格式的方法

程序员文章站 2023-11-14 08:21:04
json数据格式解析我自己分为两种; 一种是普通的,一种是带有数组形式的; 普通形式的:服务器端返回的json数据格式如下: 复制代码 代码如下:{"userbean...

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。