java中获取json中的数组转化为List<E>
java中获取json中的数组转化为List
//循环取到records的数值 for (int i = 0; i < records.size(); i++) { Device item = JSONObject.parseObject(JSONObject.toJSONString(records.get(i)), Device.class); //取到设备id String id = item.getId(); //该字段是json数组这样获取 JSONArray alarmThreshold = JSONArray.parseArray(item.getAlarmThreshold()); //判空处理 if (alarmThreshold != null && alarmThreshold.size() > 0) { //该字段为多个json数组虚幻取出单个 for (int h = 0; h < alarmThreshold.size(); h++) { //获取到该字段全部数值 String alarmThresholds = String.valueOf(JSONObject.parseObject(alarmThreshold.getString(h)).get("data")); //此处存入到list中 List<AlarmThresholdData> alarmThresholdData = JSONObject.parseObject(alarmThresholds, ArrayList.class); //alarmThresholdData 此处改字段已经是单个data,循环遍历该单个data for (int a = 0; a < alarmThresholdData.size(); a++) {AlarmThresholdData items = JSONObject.parseObject(JSONObject.toJSONString(alarmThresholdData.get(a)), AlarmThresholdData.class);//取出该json字段中具体属性值String alarmLvId = items.getAlarmLvId();String value = items.getValue();String conditional = items.getConditional();
重要方法转:
获取到的json字段数据库为json数组
JSONArray alarmThreshold = JSONArray.parseArray(item.getAlarmThreshold());
对该数组进行判空处理,继续循环遍历。取出json数组单个json数据。存入到list中
if (alarmThreshold != null && alarmThreshold.size() > 0) { for (int h = 0; h < alarmThreshold.size(); h++) { String alarmThresholds = String.valueOf(JSONObject.parseObject(alarmThreshold.getString(h)).get("data")); List<AlarmThresholdData> alarmThresholdData = JSONObject.parseObject(alarmThresholds, ArrayList.class);
循环遍历该List取出Json数组里面具体的单个字段值
for (int a = 0; a < alarmThresholdData.size(); a++) {AlarmThresholdData items = JSONObject.parseObject(JSONObject.toJSONString(alarmThresholdData.get(a)), AlarmThresholdData.class);String alarmLvId = items.getAlarmLvId();String value = items.getValue();String conditional = items.getConditional();