json解析

json数据因其简洁省流量,可在网上快速传输的特点,获得广泛使用。android的api中也提供了解析json的类和方法,相比于xml,其代码编写简单,解析速度也很快。

普通形式的:
服务器端返回的json数据格式如下:
有两个花括号
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

如下是气象局的数据(更新缓慢,近乎失效):

{"weatherinfo":{"city":"西安","city_en":"xian","date_y":"2014年2月19日","date":"","week":"星期三","fchh":"11","cityid":"101110101","temp1":"9℃~-3℃","temp2":"10℃~0℃","temp3":"7℃~0℃","temp4":"10℃~1℃","temp5":"10℃~1℃",

"temp6":"8℃~2℃",

"tempF1":"48.2℉~26.6℉","tempF2":"50℉~32℉","tempF3":"44.6℉~32℉","tempF4":"50℉~33.8℉","tempF5":"50℉~33.8℉","tempF6":"46.4℉~35.6℉",

"weather1":"晴","weather2":"晴转阴","weather3":"阴","weather4":"多云","weather5":"阴转小雨","weather6":"小雨转阴","img1":"0","img2":"99","img3":"0","img4":"2","img5":"2","img6":"99","img7":"1","img8":"99","img9":"2","img10":"7","img11":"7","img12":"2",

"img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"阴","img_title5":"阴","img_title6":"阴","img_title7":"多云","img_title8":"多云","img_title9":"阴","img_title10":"小雨","img_title11":"小雨","img_title12":"阴","img_title_single":"晴","wind1":"东北风小于3级","wind2":"东北风小于3级","wind3":"东北风小于3级","wind4":"东北风小于3级","wind5":"东北风小于3级","wind6":"东北风小于3级","fx1":"东北风","fx2":"东北风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"冷","index_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index48":"冷","index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index_uv":"中等","index48_uv":"弱","index_xc":"适宜","index_tr":"适宜","index_co":"较舒适","st1":"8","st2":"-2","st3":"9","st4":"-2","st5":"8","st6":"-2","index_cl":"适宜","index_ls":"基本适宜","index_ag":"极不易发"}}

使用如下代码:

                JSONObject jsonObject = new JSONObject(result)//result为String类型的json数据 
                        .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}
]
}
}

分析代码如下:

            JSONObject jsonObject = new JSONObject(result) 
                            .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); 
                    }

 

posted @ 2014-02-27 18:47  康行天下  阅读(341)  评论(0编辑  收藏  举报