android 解析json数据格式的方法

android 解析json数据格式的方法,第1张

概述json数据格式解析我自己分为两种;一种是普通的,一种是带有数组形式的;普通形式的:服务器端返回的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) {
                    /*
                     * 当返回码为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)","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。

总结

以上是内存溢出为你收集整理的android 解析json数据格式的方法全部内容,希望文章能够帮你解决android 解析json数据格式的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存