第一次插入比较难的JSON

复杂的json的转换及插入

1.json例子

{
   "error_msg": "0",
   "data": {
       "yAxis": [
          [
               0.00,
               0.01,
               59.97,
               34.42,
               11.73,
               11.16,
               10.61,
               0.62
          ],
          [
               17.06,
               16.99,
               23.31,
               25.98,
               17.62,
               6.13,
               23.19,
               7.88
          ],
          [
               0.00,
               0.00,
               9.77,
               8.56,
               3.23,
               1.11,
               3.42,
               0.25
          ],
          [
               0.00,
               0.01,
               50.20,
               25.86,
               8.50,
               10.05,
               7.19,
               0.37
          ],
          [
               17.06,
               16.99,
               13.54,
               17.42,
               14.39,
               5.02,
               19.77,
               7.63
          ]
      ],
       "updateDate": "",
       "xAxis": [
           "05",
           "06",
           "07",
           "08",
           "09",
           "10",
           "11",
           "12"
      ],
       "selfUseData": {
           "pvElec": 128.52,
           "useElec": 147.30,
           "buyElec": 144.51,
           "sellElec": 125.73,
           "chargeElec": 0,
           "dischargeElec": 0,
           "pvSelfConsumedRate": 0.022,
           "loadSelfConsumedRate": 0.019,
           "pvSelfConsumedElec": 2.79,
           "loadSelfConsumedElec": 2.79,
           "sellRate": 0.978,
           "buyRate": 0.981
      }
  },
   "error_code": "0"
}

 

 

2.插入到数据库的格式

INSERT INTO `` (`plant_uid`, `wisdom_device_sn`, `pv_tel`, `load_tel`, `auto_tel`, `sell_tel`, `buy_tel`, `day`, `chart_date_type`, `chart_month`) VALUES ('1150E0FB-0D1D-4051-BC32-4133BB72214E', 'M5440G2212000390', '48.77', '78.93', '15.77', '33', '63.16', '01', '2', '2022-07');

上面数据不一致,有多少天,这个pland_uid就有多少条。

 

 

3.java的写法

重点提醒,JSONArray是获取json里面的json数组,JSONObject,是获取,标准的键值对,一般都是JSONobject

package com.cx.core.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.core.entity.BtoOperationDay;
import com.cx.core.entity.VWisdomSn;
import com.cx.core.mapper.BtoOperationDayMapper;
import com.cx.core.mapper.VWisdomSnMapper;
import com.cx.core.service.BtoOperationDayService;
import com.cx.core.utils.ApiParamUtil;
import com.cx.core.utils.SajAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class BtoOperationDayServiceImpl extends ServiceImpl<BtoOperationDayMapper, BtoOperationDay> implements BtoOperationDayService {


   @Autowired
   private VWisdomSnMapper vWisdomSnMapper;



   @Override
   public Integer insterOperationDay() {

       QueryWrapper<VWisdomSn> queryWrapper = new QueryWrapper<>();
       int count = 0;
       String chartMonth = "2022-07";    //调的7月的
       List<VWisdomSn> lists = vWisdomSnMapper.selectList(queryWrapper);   //从创建好的实体,那plandId和WisdomDeviceSn
       for (VWisdomSn list : lists) {
           String plantUid = list.getPlantUid();
           String wisdomDeviceSn = list.getWisdomDeviceSn();

           String listResult = HttpUtil.createGet(SajAPI.BASE_URL + SajAPI.BOTONG_PLANT_WD_CHARTDATA)
                  .timeout(1000 * 600)
                  .addHeaders(ApiParamUtil.getToken())
                  .form(ApiParamUtil.nverterDatasigns_month(plantUid, wisdomDeviceSn, 2, chartMonth, "5D6E561D81A8E618D2C5C63AB4BAB01052B8"))
                  .execute()
                  .body();   //获取的链接的数据

           JSONArray yAxis = JSONUtil.parseObj(listResult).getJSONObject("data").getJSONArray("yAxis");//求data下面的yaxis的数据
           Object[] objects = yAxis.toArray(); //转化为对象数组


           JSONArray xAxis = JSONUtil.parseObj(listResult).getJSONObject("data").getJSONArray("xAxis");//求data下面的xaxis的数据
           if (xAxis == null) {    
               String s = "";
          } else {
               String s = xAxis.toString();  //转化为字符串
               String sub = StrUtil.sub(s, 1, -1); //去掉两边的中括号
               String replace = sub.replace("\"", "");  //去掉双引号
               String[] split_xs = replace.split("\""); //去掉引号,好像操作重复了

               StringBuffer buffer = new StringBuffer(); //下面就是将字符串数组,转为字符串
               for (int i = 0; i < split_xs.length; i++) {
                   buffer.append(split_xs[i]);  //使用循环的方式
              }
               String xAxisArr = buffer.toString();//转为字符串


               JSONObject selfUseData = JSONUtil.parseObj(listResult).getJSONObject("data").getJSONObject("selfUseData");
//获取data下面的selfusedata
               BtoOperationDay day = new BtoOperationDay();  //new了一个对象,为后续插入做准备
               Iterator<String> iterator = selfUseData.keySet().iterator(); //迭代器,用以遍历,确定key也就确定了value,keySet用于获取json对象中的主键。

               String[] j = new String[12];  //因为已经确定selfusedate,有12个元素。
               for (int i = 0; i < 12; i++) {
                   String key = iterator.next();//for循环下遍历迭代器
                   String value = selfUseData.getStr(key);
                   j[i] = value;
              }

               day.setPlantUid(plantUid);  //将上面从视图里面取出来的的uid,set到uid里面去
               day.setWisdomDeviceSn(wisdomDeviceSn);//同理
               day.setChartDateType("2");//因为求的是月天
               day.setChartMonth(chartMonth);//同理

               String s0 = objects[0].toString();    //y轴0
               String s1 = objects[1].toString();    //y轴1
               String s2 = objects[2].toString();    //y轴2
               String s3 = objects[3].toString();    //y轴3
               String s4 = objects[4].toString();    //y轴4
               String sub0 = StrUtil.sub(s0, 1, -1);//去括号
               String sub1 = StrUtil.sub(s1, 1, -1);
               String sub2 = StrUtil.sub(s2, 1, -1);
               String sub3 = StrUtil.sub(s3, 1, -1);
               String sub4 = StrUtil.sub(s4, 1, -1);

               String[] y0 = sub0.split(","); //以逗号切割
               String[] y1 = sub1.split(",");
               String[] y2 = sub2.split(",");
               String[] y3 = sub3.split(",");
               String[] y4 = sub4.split(",");
               String[] x = xAxisArr.split(",");



               int y0Length = y0.length;//只需要求一个下标,因为一个月的天数确定,所以这里的下标都一样
               for (int i = 0; i < y0Length; i ++) {  //循环插入
                   day.setPvTel(y0[i]);
                   day.setLoadTel(y1[i]);
                   day.setAutoTel(y2[i]);
                   day.setSellTel(y3[i]);
                   day.setBuyTel(y4[i]);
                   day.setDay(x[i]);
                   baseMapper.insert(day);
                   count++;
              }

          }

      }
       System.out.println("结束!" + count);
       return count;
  }
}
 
posted @ 2022-08-12 15:58  锦书南辞  阅读(77)  评论(0编辑  收藏  举报