JSON 数据解析的一些事

1.JSONObject 及 JSONArray 取值

1.1 String 转 JSONObject 或 JSONArray

JSONArray jsonArray = JSONArray.fromObject(str);
JSONObject jsonObject = JSONObject.fromObject(str);

1.2 JSONObject 中获取 JSONArray 或 String

JSONObject jsonObject = new JSONObject(resource);
String str = jsonObject.getString("str");
//获取 sequences的值
JSONArray sequencesArray = jsonObject.getJSONArray("sequences");

JSONObject 中还有其它 get 方法。

1.3 JSONArray 中获取 JSONObject 或 String

JSONArray jsonArray = new JSONArray(resource);
String str = jsonArray.getString(0);
JSONObject jsonObject = jsonArray.getJSONObject(0);

解析 json 格式的数据时要看清楚,最外面为 { 的是 JSONObject ,为 [ 的是JSONArray。

1.4 例子

1.4.1 JSONArray 中获取 JSONObject

public class Province extends DataSupport {

    private int id;

    private String provinceName;

    private int provinceCode;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public int getProvinceCode() {
        return provinceCode;
    }

    public void setProvinceCode(int provinceCode) {
        this.provinceCode = provinceCode;
    }
}
JSON 数组 [{"id":1, "name":北京},{"id":2, "name":上海},{"id":3, "name":天津},{"id":4, "name":重庆},{"id":5, "name":香港}]
public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

1.4.2 JSONObject 中获取 JSONArray,然后在从 JSONArray 中获取 JSONObject

public class Weather {

    public String status;

    public Basic basic;

    public AQI aqi;

    public Now now;

    public Suggestion suggestion;

    @SerializedName("daily_forecast")
    public List forecastList;

}
{"HeWeather": [{"basic":{"cid":"CN101300105","location":"隆安","parent_city":"南宁","admin_area":"广西","cnty":"中国","lat":"23.17476273","lon":"107.68865967","tz":"+8.00","city":"隆安","id":"CN101300105","update":{"loc":"2018-05-02 11:47","utc":"2018-05-02 03:47"}},"update":{"loc":"2018-05-02 11:47","utc":"2018-05-02 03:47"},"status":"ok","now":{"cloud":"25","cond_code":"300","cond_txt":"阵雨","fl":"32","hum":"71","pcpn":"0.0","pres":"1014","tmp":"29","vis":"10","wind_deg":"107","wind_dir":"东南风","wind_sc":"1","wind_spd":"4","cond":{"code":"300","txt":"阵雨"}},"daily_forecast":[{"date":"2018-05-02","cond":{"txt_d":"中雨"},"tmp":{"max":"30","min":"23"}},{"date":"2018-05-03","cond":{"txt_d":"阵雨"},"tmp":{"max":"27","min":"22"}},{"date":"2018-05-04","cond":{"txt_d":"阵雨"},"tmp":{"max":"28","min":"23"}}],"hourly":[{"cloud":"90","cond_code":"305","cond_txt":"小雨","dew":"26","hum":"83","pop":"58","pres":"1012","time":"2018-05-02 13:00","tmp":"30","wind_deg":"188","wind_dir":"南风","wind_sc":"1-2","wind_spd":"11"},{"cloud":"95","cond_code":"305","cond_txt":"小雨","dew":"24","hum":"85","pop":"49","pres":"1010","time":"2018-05-02 16:00","tmp":"30","wind_deg":"172","wind_dir":"南风","wind_sc":"1-2","wind_spd":"1"},{"cloud":"98","cond_code":"305","cond_txt":"小雨","dew":"24","hum":"87","pop":"25","pres":"1010","time":"2018-05-02 19:00","tmp":"29","wind_deg":"173","wind_dir":"南风","wind_sc":"1-2","wind_spd":"8"},{"cloud":"99","cond_code":"305","cond_txt":"小雨","dew":"23","hum":"89","pop":"20","pres":"1013","time":"2018-05-02 22:00","tmp":"28","wind_deg":"27","wind_dir":"东北风","wind_sc":"1-2","wind_spd":"9"},{"cloud":"99","cond_code":"104","cond_txt":"阴","dew":"22","hum":"90","pop":"71","pres":"1012","time":"2018-05-03 01:00","tmp":"25","wind_deg":"5","wind_dir":"北风","wind_sc":"1-2","wind_spd":"6"},{"cloud":"100","cond_code":"306","cond_txt":"中雨","dew":"22","hum":"93","pop":"49","pres":"1012","time":"2018-05-03 04:00","tmp":"24","wind_deg":"2","wind_dir":"北风","wind_sc":"1-2","wind_spd":"6"},{"cloud":"100","cond_code":"104","cond_txt":"阴","dew":"22","hum":"93","pop":"34","pres":"1013","time":"2018-05-03 07:00","tmp":"23","wind_deg":"11","wind_dir":"北风","wind_sc":"1-2","wind_spd":"10"},{"cloud":"100","cond_code":"305","cond_txt":"小雨","dew":"22","hum":"86","pop":"70","pres":"1015","time":"2018-05-03 10:00","tmp":"23","wind_deg":"3","wind_dir":"北风","wind_sc":"1-2","wind_spd":"2"}],"aqi":{"city":{"aqi":"51","pm25":"29","qlty":"良"}},"suggestion":{"comf":{"type":"comf","brf":"较舒适","txt":"白天有雨,从而使空气湿度加大,会使人们感觉有点儿闷热,但早晚的天气很凉爽、舒适。"},"sport":{"type":"sport","brf":"较不宜","txt":"有较强降水,建议您选择在室内进行健身休闲运动。"},"cw":{"type":"cw","brf":"不宜","txt":"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"}}}]}
    /**
     * 将返回的 JSON 数据解析成 Weather 实体类
     */
    public static Weather parseWeatherResponse(String response) {
        try {

            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();

            return new Gson().fromJson(weatherContent, Weather.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(JSON 数据解析的一些事)