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