JSON分组

package com.mhw.json.group.test;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: lomir-ai-box
 * @description: Json分组示例
 * @author: ext.manhengwei1
 * @create: 2023-06-09 09:08
 **/
public class JsonGroupingExample {
    /**
     * 主函数,读取Excel文件数据并进行处理
     *
     * @param args main函数参数
     * @throws IOException 文件读取异常
     */
    public static void main(String[] args) throws IOException {
        // Excel文件路径
        String fileName = "C:\\Users\\ext.manhengwei1\\Desktop\\格式一模版.xlsx";
        // 将文件路径转换为Spring框架MultipartFile类型
        MultipartFile file = filePath2MultipartFile(fileName);
        // 读取Excel文件数据并将其转换为List<Map>形式
        List<Map<String, String>> dataList = EasyExcel.read(file.getInputStream()).sheet().doReadSync();
        // 去掉前两行数据
        dataList.subList(0, 2).clear();
        // 遍历数据列表,将Map中key的类型转换为String类型
        for (Map<String, String> map : dataList) {
            Map<String, String> newMap = new HashMap<>(map.size());
            for (Map.Entry<String, String> entry : map.entrySet()) {
                newMap.put(String.valueOf(entry.getKey()), entry.getValue());
            }
            map.clear();
            map.putAll(newMap);
        }
        // 将List<Map>转换为JSON数组
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(dataList));
        // 存放处理后的人员信息列表
        List<JSONObject> personList = new ArrayList<>();
        // 当前正在处理的人员信息
        JSONObject currentPerson = null;
        // 遍历JSON数组
        for (int i = 0; i < jsonArray.size(); i++) {
            // 获取当前对象
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // 如果当前对象中包含key为"0"的数据,则认为该对象是新的人员信息
            if (jsonObject.containsKey("0")) {
                // 如果当前正在处理的人员信息不为null,则将其添加到列表中
                if (currentPerson != null) {
                    personList.add(currentPerson);
                }
                // 新建一个人员信息,并添加childData属性
                currentPerson = jsonObject;
                currentPerson.put("childData", new ArrayList<>());
                // 获取childData属性列表
                List<JSONObject> childData = currentPerson.getObject("childData", List.class);
                // 存放需要删除的key列表
                List<String> keysToRemove = new ArrayList<>();
                // 新建一个JSON对象,用于存放子项数据
                JSONObject child = new JSONObject();
                // 遍历当前人员信息,将key不为"childData",且下标大于等于23的数据添加到child对象中
                for (String key : currentPerson.keySet()) {
                    if (!"childData".equals(key) && Integer.parseInt(key) >= 23) {
                        if (!"实施".equals(currentPerson.getString(key)) && !"不实施".equals(currentPerson.getString(key))) {
                            keysToRemove.add(key);
                            child.put(key, currentPerson.getString(key));
                            // 将child对象添加到childData列表中
                            if (child.size() > 0) {
                                if (childData.size() > 0 && childData.get(childData.size() - 1).equals(child)) {
                                    childData.get(childData.size() - 1).putAll(child);
                                } else {
                                    childData.add(child);
                                }
                            }
                        }
                    }
                }
                // 删除key
                keysToRemove.forEach(currentPerson::remove);
            } else {
                // 当前对象属于前一个人的数据,将其添加到前一个对象的列表属性中
                if (currentPerson != null) {
                    List<JSONObject> childData = currentPerson.getObject("childData", List.class);
                    childData.add(jsonObject);
                }
            }
        }
        // 将最后一个人员信息添加到列表中
        if (currentPerson != null) {
            personList.add(currentPerson);
        }
        // 遍历人员信息列表,输出每个人员信息的JSON字符串
        for (JSONObject person : personList) {
            System.out.println(JSON.toJSONString(person));
        }
    }

    private static MultipartFile filePath2MultipartFile(String fileName) throws IOException {
        File file = new File(fileName);
        FileInputStream input = new FileInputStream(file);
        return new MockMultipartFile(file.getName(),
                file.getName(), "text/plain", input);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Vue进行分组过滤JSON对象的方法: 1. 首先,你需要在Vue中引入一个名为“vue-json-tree-view”的组件。你可以通过以下命令进行安装: ```shell npm install vue-json-tree-view --save ``` 2. 在Vue组件中,你需要引入该组件并将JSON对象传递给它。你可以使用以下代码: ```html <template> <div> <json-tree :data="jsonData"></json-tree> </div> </template> <script> import JsonTree from 'vue-json-tree-view' export default { components: { JsonTree }, data () { return { jsonData: { // your JSON data here } } } } </script> ``` 3. 然后,你需要使用Vue的计算属性来对JSON对象进行分组。你可以使用以下代码: ```html <template> <div> <json-tree :data="groupedData"></json-tree> </div> </template> <script> import JsonTree from 'vue-json-tree-view' export default { components: { JsonTree }, data () { return { jsonData: { // your JSON data here } } }, computed: { groupedData () { // group your JSON data here return groupedData } } } </script> ``` 4. 最后,你需要在Vue组件中使用过滤器来过滤JSON对象。你可以使用以下代码: ```html <template> <div> <json-tree :data="filteredData"></json-tree> </div> </template> <script> import JsonTree from 'vue-json-tree-view' export default { components: { JsonTree }, data () { return { jsonData: { // your JSON data here }, filter: 'your filter here' } }, computed: { filteredData () { // filter your JSON data here return filteredData } }, filters: { filterData (data, filter) { // your filter function here return filteredData } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值