解析json數據

這裏寫圖片描述
這裏寫圖片描述

1.根據這樣的json數據,寫成的相應的bean類型
public class SmartControlBean {

    // Status 0
    // getDeviceByMobile Array
    public List<BLEDeviceInformationBean>   getDeviceByMobile;
    public int                  Status;

    /**
     * 這是具體要控制藍牙設備的信息
     */
    public class BLEDeviceInformationBean {
        // deviceAddress D4F513FF3DC5
        // type 2
        public String   deviceAddress;
        public String       type;
    }
}
2.把服務器獲取到的json數據解析出來
/**
     * 解析json數據
     * 
     * @param json
     */
    private void processJson(String json) {
        // 解析json
        Gson gson = new Gson();
        mBean = gson.fromJson(json, SmartControlBean.class);

        if (mBean.Status != 0) {
            return;
        } else {

            for (int i = 0; i < mBean.getDeviceByMobile.size(); i++) {

                // 獲取設備所屬type和MAC地址deviceAddress
                mBLEDeviceMACAddress = mBean.getDeviceByMobile.get(i).deviceAddress;
                mBLEDeviceType = mBean.getDeviceByMobile.get(i).type;
                Log.v(TAG, "設備類型:" + mBLEDeviceType + "    " + "設備的MAC地址:"
                        + mBLEDeviceMACAddress);

                // 把type和deviceAddress存儲到本地
                // 從0~10分別對應(23:電視 22:空調 24:門鎖 10:房燈 11:牀燈1 12:牀燈2 13:牀燈3 14:廊燈
                // 15:廁所燈 16:浴室燈 21:窗簾)
                mPreferenceHelper = PreferenceHelper.getInstance(mContext, null);
                mPreferenceHelper.setString("type" + i, mBLEDeviceType);
                mPreferenceHelper.setString("deviceAddress" + i, mBLEDeviceMACAddress);
            }
        }
    }

說明:json是從服務器下載下來的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章