没有带引号的

    @ApiOperation("导入盒须图数据4")
    @PostMapping("/importData4")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData4(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 提取数据
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
        int sizeLine = getdata.size();
        System.out.println("我是有多少列" + sizeLine);
        // 打印原始数据
        for (List<String> getdatum : getdata) {
            System.out.println(getdatum);
        }
        // 获取第一行数据(数据名行)
        List<String> strings = getdata.get(0);
        int previousNonZeroIndex = -1;
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 1; i < strings.size(); i++) {
            String value = strings.get(i);
            if (!value.equals("0")) {
                if (previousNonZeroIndex != -1) {
                    int columnsBetween = i - previousNonZeroIndex;
                    arrayList.add(columnsBetween);
                }
                previousNonZeroIndex = i;
            }
        }

        // 移除第一行和第二行(如果需要)
        getdata.remove(0);
        getdata.remove(0);
        Object arr0 = arrayList.get(0);
        Integer integerValue = (Integer) arr0;
        int line = integerValue;
        System.out.println("我是中间隔的列数" + line);
        int number = sizeLine / line; //列的数据
        // 创建一个ArrayList的List来存储每一列的数据
        List<List<Double>> columnData = new ArrayList<>();
        // 初始化columnData,假设getdata不为空且至少有一行数据
        if (!getdata.isEmpty()) {
            int numColumns = getdata.get(0).size() - 1; // 减去1,因为我们要移除每行的第一列
            for (int i = 0; i < numColumns; i++) {
                columnData.add(new ArrayList<>());
            }
        }

        // 获取seriesName列表
        ArrayList<String> seriesName = new ArrayList<>();

        // 遍历excel的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String firstColumnValue = row.remove(0); // 移除第一列并获取其值
            seriesName.add(firstColumnValue);

            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                try {
                    double numericValue = Double.parseDouble(cellValue);
                    columnData.get(colIndex).add(numericValue);
                } catch (NumberFormatException e) {
                    System.err.println("Invalid number format: " + cellValue);
                }
            }
        }

        ArrayList<Object> arrayLists = new ArrayList<>();
        for (int colIndex = 0; colIndex < columnData.size(); colIndex++) {
            arrayLists.add(columnData.get(colIndex));
        }

        // 分割 arrayLists
        List<List<Object>> groupedLists = splitList(arrayLists, line);

        // 打印分组后的数据
        for (int i = 0; i < groupedLists.size(); i++) {
            System.out.println("Group " + (i + 1) + ": " + groupedLists.get(i));
        }

        // 以下操作json
        String chartValue = chartDateTto.getChartValue();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(chartValue);
        ArrayNode dataset = (ArrayNode) rootNode.path("dataset");

        // 查找并保留第一个含有 source 的对象,删除其他含有 source 的对象
        JsonNode firstSourceNode = null;
        int firstSourceNodeIndex = -1;
        for (int i = 0; i < dataset.size(); i++) {
            JsonNode node = dataset.get(i);
            if (node.has("source")) {
                if (firstSourceNode == null) {
                    // 记录第一个含有 source 的对象,并保存其索引
                    firstSourceNode = node;
                    firstSourceNodeIndex = i;
                } else {
                    // 删除其他含有 source 的对象
                    dataset.remove(i);
                    i--; // 调整索引,因为我们删除了一个元素
                }
            }
        }

        // 根据 groupedLists.size() 的值复制 firstSourceNode,并添加到 dataset 中
        int requiredDatasets = groupedLists.size();
        int insertIndex = firstSourceNodeIndex + 1; // 插入位置在第一个 source 对象之后
        for (int i = 1; i < requiredDatasets; i++) { // 从1开始,因为第一个已经存在
            dataset.insert(insertIndex, firstSourceNode.deepCopy());
            insertIndex++; // 每次插入后,更新插入位置
        }

        // 更新 dataset 中每个 source 对象的 source 数组值
        int groupIndex = 0;
        for (JsonNode node : dataset) {
            if (node.has("source")) {
                ArrayNode sourceArray = (ArrayNode) node.get("source");

                // 确保 sourceArray 与 groupedLists 的大小一致
                sourceArray.removeAll();
                List<Object> groupData = groupedLists.get(groupIndex);
                for (Object item : groupData) {
                    // 将每个项转换为 JsonNode
                    ArrayNode arrayNode = objectMapper.createArrayNode();
                    ((List<Double>) item).forEach(arrayNode::add);
                    sourceArray.add(arrayNode);
                }
                groupIndex++;
            }
        }

        // 打印更新后的 dataset
        System.out.println("更新后的 dataset: " + dataset.toString());

        // 打印完整的 JSON 数据
        String updatedJson = rootNode.toString();
        System.out.println("完整的 JSON 数据: " + updatedJson);

        return AjaxResult.success();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值