android json如何解析?

解决方案 »

  1.   

    JSONObject,这个跟普通java解析json没区别的呀
      

  2.   

    还是有区别的,安卓里面解析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();


    String week1 = getWeekDay(date_y);// 今天周几
    String week2 = getTomorrowWeekDay(date_y);
    String week3 = getAtWeekDay(date_y);

    String img1 = weatherday.get("img1").toString();
    String img2 = weatherday.get("img2").toString();
    String img3 = weatherday.get("img3").toString();
    String img4 = weatherday.get("img4").toString();
    String img5 = weatherday.get("img5").toString();
    String img6 = weatherday.get("img6").toString(); String temp1 = weatherday.get("temp1").toString();// 今天温度
    String temp2 = weatherday.get("temp2").toString();// 明天温度
    String temp3 = weatherday.get("temp3").toString();// 后天温度 String weather1 = weatherday.get("weather1").toString();// 今天温度
    String weather2 = weatherday.get("weather2").toString();// 明天温度
    String weather3 = weatherday.get("weather3").toString();// 后天温度

    String wind1 = weatherday.get("wind1").toString();//今天风力
    String wind2 = weatherday.get("wind2").toString();//明天风力
    String wind3 = weatherday.get("wind3").toString();//后天风力

    //城市共有信息
    cityWeather.setCityname(city);
    cityWeather.setCity_en(city_en);
    cityWeather.setCityid(cityid);
    cityWeather.setDate_y(date_y);

    //获得星期
    cityWeather.setToddayWeek(week1);
    cityWeather.setTommorrowWeek(week2);
    cityWeather.setAtWeek(week3);

    //获得图片(白天)
    cityWeather.setTodayImg1(img1);
    cityWeather.setTommorrowImg1(img3);
    cityWeather.setAtImg1(img5);
    // System.out.println("img1,img3,img5,"+img1+","+img3+","+img5);

    //获得图片(夜间)
    cityWeather.setTodayImg2(img2);
    cityWeather.setTommorrowImg2(img4);
    cityWeather.setAtImg2(img6);
    // System.out.println("img2,img4,img6,"+img2+","+img4+","+img6);

    //获得温度
    cityWeather.setTodayTemp(temp1);
    cityWeather.setTommorrowTemp(temp2);
    cityWeather.setAtTemp(temp3);

    //获得天气
    cityWeather.setTodayWeather(weather1);
    cityWeather.setTommorrowWeather(weather2);
    cityWeather.setAtWeather(weather3);

    //获得风力
    cityWeather.setTodayWind(wind1);
    cityWeather.setTommorrowWind(wind2);
    cityWeather.setAtWind(wind3);

    } catch (Exception e) {
    return null;
    }
    return cityWeather;
    }