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();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值