解析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是从服务器下载下来的数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值