excel转json (树状结构)

一、python读取excel 转json

目的:在于将excel的数据转换成json格式。

import xlrd, jsondef read_xlsx_file(filename):# 打开Excel文件data = xlrd.open_workbook(filename)# 读取第一个工作表table = data.sheets()[0]# 统计行数rows = table.nrowsdata = []  # 存放数据for i in range(1, rows):values = table.row_values(i)data.append(({"kbName": str(str(values[0])),"cateOne": values[1],"cateTwo": values[2],"cateThree": values[3],"cateFour": values[4],}))return dataif __name__ == '__main__':d1 = read_xlsx_file("新建 XLS 工作表.xls")# 字典中的数据都是单引号,但是标准的json需要双引号js = json.dumps(d1, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ':'))print(js)# 前面的数据只是数组,加上外面的json格式大括号js = "{" + js + "}"# 可读可写,如果不存在则创建,如果有内容则覆盖jsFile = open("./text3.json", "w+", encoding='utf-8')jsFile.write(js)jsFile.close()

二、java 转成响应的树状结构
虽然读取的是json格式,但是没有读成树状结构,因为python不熟悉,所以用java改成树状结构。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class TansTo {@Testpublic void toTrans() {String jsonstring = "[\n" +"    {\n" +"        \"cateFour\":\"两全\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    },\n" +"    {\n" +"        \"cateFour\":\"终身\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    },\n" +"    {\n" +"        \"cateFour\":\"定期\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    },\n" +"    {\n" +"        \"cateFour\":\"健康\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    },\n" +"    {\n" +"        \"cateFour\":\"车金\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    },\n" +"    {\n" +"        \"cateFour\":\"其他\",\n" +"        \"cateOne\":\"产品\",\n" +"        \"cateThree\":\"产品分类\",\n" +"        \"cateTwo\":\"个险活动方案\",\n" +"        \"kbName\":\"产品_个险活动\"\n" +"    }\n" +"]";JSONArray jsonArray = JSONObject.parseArray(jsonstring);List<TransMid> transMids = jsonArray.toJavaList(TransMid.class);System.out.println(transMids);//转目标格式//获取kbName的去重listList<String> collect = transMids.stream().map(TransMid::getKbName).distinct().collect(Collectors.toList());System.out.println(collect);List<KnowledgeBaseInfo> knowledgeBaseInfoList = new ArrayList<>();for (TransMid transMid : transMids){//kbName层//查找已添加的kb的NameList<String> kbNames = knowledgeBaseInfoList.stream().map(kbinf -> kbinf.getKnowledgeBaseName()).collect(Collectors.toList());if (!kbNames.contains(transMid.getKbName())) {KnowledgeBaseInfo knowledgeBaseInfo = new KnowledgeBaseInfo();knowledgeBaseInfo.setKnowledgeBaseName(transMid.getKbName());List<KnowledgeBaseInfo.Catesgreis> cateOneList = new ArrayList<>();//获取相同kbName的List<TransMid> samekbInfoList = transMids.stream().filter(trans -> trans.getKbName().equals(transMid.getKbName())).collect(Collectors.toList());//获取一级分类名的去重集合List<String> collectClassOne = samekbInfoList.stream().map(TransMid::getCateOne).distinct().collect(Collectors.toList());collectClassOne.stream().forEach(classOne ->{KnowledgeBaseInfo.Catesgreis catesgreis = knowledgeBaseInfo.new Catesgreis();catesgreis.setCateName(classOne);List<KnowledgeBaseInfo.Catesgreis> catesgreisListOne = new ArrayList<>();//第二级分类List<TransMid> sameClassOneList = samekbInfoList.stream().filter(trans -> trans.getCateOne().equals(classOne)).collect(Collectors.toList());List<String> collectClassTwo = sameClassOneList.stream().map(TransMid::getCateTwo).distinct().collect(Collectors.toList());collectClassTwo.stream().forEach(classTwo -> {KnowledgeBaseInfo.Catesgreis catesgreisTwo = knowledgeBaseInfo.new Catesgreis();catesgreisTwo.setCateName(classTwo);List<KnowledgeBaseInfo.Catesgreis> catesgreisListTwo = new ArrayList<>();//第三级分类List<TransMid> sameClassTwoList = sameClassOneList.stream().filter(trans -> trans.getCateTwo().equals(classTwo)).collect(Collectors.toList());List<String> collectClassThree = sameClassTwoList.stream().map(TransMid::getCateThree).distinct().collect(Collectors.toList());collectClassThree.stream().forEach(classThree -> {KnowledgeBaseInfo.Catesgreis catesgreisThree = knowledgeBaseInfo.new Catesgreis();catesgreisThree.setCateName(classThree);List<KnowledgeBaseInfo.Catesgreis> catesgreisListThree = new ArrayList<>();//第四级分类List<TransMid> sameClassThreeList = sameClassTwoList.stream().filter(trans -> trans.getCateThree().equals(classThree)).collect(Collectors.toList());List<String> collectClassFour = sameClassThreeList.stream().map(TransMid::getCateFour).distinct().collect(Collectors.toList());collectClassFour.stream().forEach(classFour -> {KnowledgeBaseInfo.Catesgreis catesgreisFour = knowledgeBaseInfo.new Catesgreis();catesgreisFour.setCateName(classFour);//                                List<KnowledgeBaseInfo.Catesgreis> catesgreisListFour = new ArrayList<>();
//                                //第五级
//                                List<TransMid> sameClassFourList = sameClassThreeList.stream().filter(trans -> trans.getCateFour().equals(classFour)).collect(Collectors.toList());
//                                sameClassFourList.stream().map(TransMid::getCateFour)catesgreisFour.setCateList(new ArrayList<>());catesgreisListThree.add(catesgreisFour);});catesgreisThree.setCateList(catesgreisListThree);catesgreisListTwo.add(catesgreisThree);});catesgreisTwo.setCateList(catesgreisListTwo);catesgreisListOne.add(catesgreisTwo);});catesgreis.setCateList(catesgreisListOne);//添加一级的listcateOneList.add(catesgreis);});knowledgeBaseInfo.setCateList(cateOneList);knowledgeBaseInfoList.add(knowledgeBaseInfo);}}System.out.println(knowledgeBaseInfoList);System.out.println(JSONArray.toJSONString(knowledgeBaseInfoList));}}

excel转json (树状结构)相关推荐

  1. 树状结构大数据类型的高效支持

    树状结构大数据类型的高效支持 陈世敏 中国科学院计算技术研究所,北京 100190 摘要:传统的关系数据模型难以满足大数据应用日益丰富的数据表达和处理的需求,因此实践中涌现了多种非传统的大数据类型.其 ...

  2. 【JAVA】读取excel导入数据库,形成树状结构

    最近需要导入一个excel表格,存到数据库并以树状结构读取出来 下面两张图片是需要导入的excel @Transactional(rollbackFor = Exception.class)publi ...

  3. 【java工具类】四级菜单如何实现树状结构展示JSON给前端

    前言: 主要是传给前端进行树状结构操作比较好,这样可以关联上下四级选择项,所以需要把表中的数据List转成一个树状结构. 1.数据库数据是静态的四级目录 数据是这样的四级目录,没有特别明确的id进行关 ...

  4. 我的前端工具集(四)树状结构后篇

    我的前端工具集(四)树状结构后篇   liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 上文连接 我的前端工具集(四)树状结构前偏 1.数据组织 在3.2.节有截图 2.树状结构代码 2 ...

  5. python 树状图代码_Python 无限级分类树状结构生成算法 「实用代码」

    def generate_tree(source, parent): tree = [] for item in source: if item["parent"] == pare ...

  6. vue项目结合iview4UI组件实现树状结构及复杂动态表头列表 Tree-Table 及复杂header 省市区树状表格联动 数据优化后台一次性返回一万条数据页面卡死问题

    一.首先看看需求最终效果图,该需求总共罗列以下几点 最左侧采用树状结构将地址省市区县街道展示出来,并且控制名称长度限制,多余的用省略号表示,鼠标悬浮上去名称展示出来 列表默认展示市一级数据,县及街道数 ...

  7. Java递归子集算法(树状结构)的逻辑和实例代码实现 @杨章隐

    Java递归算法(树状结构)的逻辑和实例 1.应用场景: 递归算法作为一个经常使用的算法,无论在API开发还是计算文件夹都是比较常用的, 在api开发过程中我们经常遇到需要返回树状结构的json 例如 ...

  8. 表格 树形结构 HTML CSS,基于jQuery ztree实现表格风格的树状结构

    zTree 简介 zTree 是一个依靠 jQuery 实现的多功能 "树插件".优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree 是开源免费的软件(M ...

  9. 从装备合成谈树状结构的应用

    源码下载: 链接: https://pan.baidu.com/s/1DucpIik596W1pkY09FjUew 提取码: n8mq 其中有个文件"类的功能介绍.txt",介绍了 ...

最新文章

  1. Java并发 -- JMM
  2. python有道翻译-使用python2爬取有道翻译
  3. 用什么DOS命令挂上*.mdf、*.ldf的数据库?
  4. 教你如何阅读Oracle数据库官方文档
  5. java中 indexOf() 与lastIndexOf() 用法详解
  6. ASP.NET Core 2.1 使用Docker运行
  7. 用node-webkit开发多平台的桌面客户端
  8. 公链项目 Taraxa 已开启公募 KYC,投资者可在 3 月 12 日申购
  9. django分页功能 views与templates
  10. 六个问题让你更懂 React Fiber
  11. 【渝粤教育】国家开放大学2019年春季 1396药事管理与法规(本) 参考试题
  12. 求出一个整型数组的最大子集和
  13. 这个AI批量作画每小时九张,与毕加索同台竞技,还真有人买
  14. 在线JS编辑器,可运行保存简单JS代码(已开源)
  15. UI设计新手怎么求职 UI面试必备攻略是什么
  16. slqdbx mysql_免费的多数据库管理工具sqldbx个人版本
  17. Raid磁盘阵列(详解,操作演示)
  18. scrollTo不起作用
  19. 用AutoCAD画尺寸链小结
  20. antd table分页,关于react的antd表格分页的问题

热门文章

  1. Professional WCF 4读书笔记(1)——集成风格
  2. 记录编译Wien2k_18.2的过程
  3. 【推荐】2021腾讯数字生态大会(共183份,演讲PPT【68份】+白皮书+解决方案+其他资料,980M)
  4. input text 设置数字最大值以及最小值
  5. 数学之美:两点之间最快的路径
  6. php图书管理系统源码详细设计,C语言程序设计:图书管理系统(超详细有登录系统,附代码和试验报告)...
  7. P2255 [USACO14JAN]记录奥林比克
  8. 用计算机制作微课教学教案,微课教学设计范文
  9. FL Studio2023最新版编曲音乐制作数字音频软件
  10. 如何在linux下查看cpu个数,linux如何查看cpu个数