Exemplo n.º 1
0
  /*
   * json数据格式的处理
   * */
  public static CityWeather dealJson(String jsonString) {
    CityWeather cw = null;
    try {
      JSONObject jsonObject = new JSONObject(jsonString); // 返回的数据形式是一个Object类型,所以可以直接转换成一个Object
      String errMsg = jsonObject.getString("errMsg");
      int errNum = jsonObject.getInt("errNum");

      if ("success".equals(errMsg)) {
        cw = new CityWeather();

        JSONObject retDataObject = jsonObject.getJSONObject("retData"); // 获取对象中的对象
        String city = retDataObject.getString("city"); // 得到city
        cw.setCity(city);
        String date = retDataObject.getString("date"); // 得到city
        cw.setDate(date);
        String time = retDataObject.getString("time"); // 得到city
        cw.setTime(time);
        String postCode = retDataObject.getString("postCode"); // 得到city
        cw.setPostCode(postCode);
        String weather = retDataObject.getString("weather"); // 得到city
        cw.setWeather(weather);
        String WD = retDataObject.getString("WD"); // 得到city
        cw.setWD(WD);
        String WS = retDataObject.getString("WS"); // 得到city
        cw.setWS(WS);
        int citycode = retDataObject.getInt("citycode");
        cw.setCitycode(citycode);
        int temp = retDataObject.getInt("temp");
        cw.setTemp(temp);
        int l_tmp = retDataObject.getInt("l_tmp");
        cw.setL_tmp(l_tmp);
        int h_tmp = retDataObject.getInt("h_tmp");
        cw.setH_tmp(h_tmp);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    /*
     * 这里应该添加数据库的东西
     *
     * */
    return cw;
  }