一、集合List和JsonStr的互转
1、集合list转jsonStr:
List<WarningListBean> mHealthWarningList = new ArrayList<>();
WarningListBean warningBean = new WarningListBean();
warningBean.setName(roleInfoEntity.getUsername());
warningBean.setNickName(roleInfoEntity.getNickName());
warningBean.setWarningType("疾病");
warningBean.setWearUserId(roleInfoEntity.getWearUserId());
mHealthWarningList .add(warningBean);
......
Gson gson = new Gson();
String warningListStr = gson.toJson(mHealthWarningList);
2、jsonStr转集合list:
String warningListStr = intent.getStringExtra("warningList");
//将传递的json字符串转化为list集合
Type listType = new TypeToken<ArrayList<WarningListBean>>() {
}.getType();
mWarningList = new Gson().fromJson(warningListStr, listType);
二、二维数组和json的互转:
com.alibaba.fastjson.JSONArray mHandlingInfoOuter = new com.alibaba.fastjson.JSONArray();
for (int i = 0; i < mHandlingInfoList.size(); i++) {
com.alibaba.fastjson.JSONArray jsonArrayInner = new com.alibaba.fastjson.JSONArray();
for (int j = 0; j < mHandlingInfoList.get(i).size(); j++) {
//加判断选择的“以上都没有”
if (mHandlingInfoList.get(i).get(j).equals("以上都没有")) {
//以上都没有,追加空 例如第二组问题选都没有:[["胸闷","无力"],[]]
//mHandlingInfo[i][j]="";//赋值为[""],不赋值为[null] 改用JSONArray,不赋值即为[]
} else {
//mHandlingInfo[i][j] = mHandlingInfoList.get(i).get(j);
jsonArrayInner.add(mHandlingInfoList.get(i).get(j));
}
}
mHandlingInfoOuter.add(jsonArrayInner);
}
String handlingInfoStri = JSON.toJSON(mHandlingInfoOuter).toString();
LogUtils.d("选中症状的数组str:" + handlingInfoStri);