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<Forecast> 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;
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容