当前位置: 代码迷 >> Android >> android json怎么解析
  详细解决方案

android json怎么解析

热度:68   发布时间:2016-04-28 08:08:51.0
android json如何解析?
android json如何解析?

------解决方案--------------------
JSONObject,这个跟普通java解析json没区别的呀
------解决方案--------------------
还是有区别的,安卓里面解析json必须依赖安卓内置的jar包,否则会引起类冲突的。
给你个例子吧:
"weatherinfo":{"city":"日照","city_en":"rizhao","date_y":"2013年9月12日","date":"","week":"星期四","fchh":"08","cityid":"101121501","temp1":"25℃~20℃","temp2":"27℃~22℃","temp3":"30℃~22℃","temp4":"29℃~20℃","temp5":"28℃~22℃","temp6":"28℃~21℃","tempF1":"77℉~68℉","tempF2":"80.6℉~71.6℉","tempF3":"86℉~71.6℉","tempF4":"84.2℉~68℉","tempF5":"82.4℉~71.6℉","tempF6":"82.4℉~69.8℉","weather1":"多云转小雨","weather2":"阴转多云","weather3":"晴转多云","weather4":"多云转晴","weather5":"多云转晴","weather6":"多云","img1":"1","img2":"7","img3":"2","img4":"1","img5":"0","img6":"1","img7":"1","img8":"0","img9":"1","img10":"0","img11":"1","img12":"99","img_single":"1","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-4级","wind2":"南风3-4级","wind3":"南风转西南风3-4级","wind4":"北风3-4级","wind5":"南风3-4级","wind6":"南风转东风3-4级","fx1":"东南风","fx2":"东南风","fl1":"3-4级","fl2":"3-4级","fl3":"3-4级","fl4":"3-4级","fl5":"3-4级","fl6":"3-4级","index":"舒适","index_d":"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。","index48":"热","index48_d":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","index_uv":"弱","index48_uv":"最弱","index_xc":"适宜","index_tr":"适宜","index_co":"舒适","st1":"25","st2":"16","st3":"27","st4":"20","st5":"28","st6":"20","index_cl":"较适宜","index_ls":"适宜","index_ag":"极易发"}}


public CityWeather getCityWeather(String cityid) throws IOException {
CityWeather cityWeather=null;
String weatherJson = dao.getWeatherJson(cityid);
// System.out.println(weatherJson);
try {
// 对json数组进行循环,一般应该只返回一个。
JSONTokener jsonParser = new JSONTokener(weatherJson);
JSONObject object = (JSONObject) jsonParser.nextValue();
// 接下来的就是JSON对象的操作了
JSONObject weatherday = object.getJSONObject("weatherinfo");

cityWeather = new CityWeather();
String city = weatherday.get("city").toString();
String city_en = weatherday.get("city_en").toString();
String date_y = weatherday.get("date_y").toString();