SpringBoot/Java 将MongoDB中的数据转换为json文件

项目背景:
我最近在做SpringBoot的项目,其中数据库使用的是MongoDB,前端框架用的是layui。在我使用layui的数据表格组件时,其数据接口对应的是json文件,所有我得将MongoDB中的collection转换为json文件。

MongoDB在SpringBoot中的具体配置在这里就不具体赘述了,直接进入正题

如下是存储在MongoDB中的数据,存在了paper_Info集合中。
在这里插入图片描述
建立一个实体类,对应这这个集合的名字。
类的属性要对应着collection中的索引,在写完类的属性后,alt+insert健idea自动补全getter和setter方法。

package com.hhu.bigdatasys.rtenzyme.entity;

import lombok.Data;
import lombok.ToString;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

@Data
@ToString
@Document(collection = "paper_info") //对应着MongoDB中的collection名称
public class paper_info {

    private String _id;

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    //论文所属的老师
    private String index;

    //论文的标题
    private String title;

    //作者列表
    private List<String> authors;

    //论文来源
    private String source;


    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getAuthors() {
        return authors;
    }

    public void setAuthors(List<String> authors) {
        this.authors = authors;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getTimes() {
        return times;
    }

    public void setTimes(String times) {
        this.times = times;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }


    //论文发表的时间
    private String times;

    //期刊类型

    private String database;

    //引用次数
    private int counted;

    public int getCounted() {
        return counted;
    }

    public void setCounted(int counted) {
        this.counted = counted;
    }


}

接下来先要载入fastjson的maven reporsity。在pom.xml中的dependency下加入:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>

layui数据表格的异步数据的结构要求如下:

{
  "code": 0,
  "msg": "",
  "count": 1000,
  "data": [{}, {}]
} 

其中code值为0代表着成功获取数据,count是data数据条的个数,msg是提示文本。

为了方便我们可以在Test文件中写相应的操作。

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoTest {

    final static private Logger logger = LoggerFactory.getLogger(MongoTest.class);

    @Resource
    MongoTemplate mongoTemplate;

    @Test
    public void testfind() throws IOException {
        Query qury = new Query();
        List<paper_info> paperInfoList;
        //该查询方式可以查到mongoDB中一个collection中所有的数据,返回paper_info类型的数据对象
        paperInfoList = mongoTemplate.find(new Query(new Criteria()),paper_info.class);
        //将bean类型的数据对象转化为json字符串,测试使用
        String jsonString = JSON.toJSONString(paperInfoList.get(0));
        System.out.println("Test:"+jsonString);
        //构建json对象的列表
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for(paper_info item:paperInfoList){ //构建迭代器
            try {
                String jsonStr = JSON.toJSONString(item);//将paper_info对象转换为json字符串
                JSONObject jsonObject = (JSONObject) JSON.parse(jsonStr);//再将字符串转换为json对象
                jsonObjectList.add(jsonObject);//加入到jsonObject列表中
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        JSONObject jsonContainer = new JSONObject();//构建json容器
        //根据layui数据表格的数据接口的要求构建json结构
        jsonContainer.put("msg","");
        jsonContainer.put("count",paperInfoList.size());
        jsonContainer.put("code",0);
        jsonContainer.put("data",jsonObjectList);
        System.out.println(jsonContainer);
        //将json写入到文件中
        File file = new File("src\\main\\resources\\static\\paper_info.json");
        FileWriter fileWritter = new FileWriter(file,false);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWritter);
        bufferedWriter.write(jsonContainer.toJSONString());
        bufferedWriter.close();
    }
}

结果展示

{"msg":"","code":0,"data":[{"database":"期刊","times":"2020-06-20","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7783","source":"湖北民族大学学报(医学版)","title":"纳布啡联合丙泊酚用于无痛人工流产术的效果观察","authors":["许峰","樊志龙"]},{"database":"期刊","times":"2020-06-17 15:21","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7784","source":"中国畜牧兽医","title":"猪链球菌14型的分离鉴定及生物学特性研究","authors":["王治方","徐引弟","张青娴","朱文豪","白红杰"]},{"database":"期刊","times":"2020-06-15","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7785","source":"实验力学","title":"金属铝微波烧结微结构演化的在线观测与相场模拟","authors":["胡祥瑞","许峰","胡小方","肖宇"]},{"database":"期刊","times":"2020-06-15","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7786","source":"内科急危重症杂志","title":"重组组织型纤溶酶原激活剂联合脑室外引流治疗脑室出血的临床观察","authors":["许峰","连立飞","梁奇明","张萍","王芙蓉"]},{"database":"期刊","times":"2020-06-12 09:09","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7787","source":"煤炭学报","title":"保德煤矿峰峰组隔水性能多尺度定量化评价研究","authors":["许峰","靳德武","杨俊哲","黄欢","王世东"]},{"database":"期刊","times":"2020-06-10","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7788","source":"预防医学","title":"温州市新型冠状病毒肺炎病例流行特征分析","authors":["李玲","夏子淇","余向华","倪朝荣","潘琼娇"]},{"database":"期刊","times":"2020-06-10","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e7789","source":"养猪","title":"2型猪链球菌河南株的生物学特性研究","authors":["王治方","徐引弟","张青娴","朱文豪","焦文强"]},{"database":"报纸","times":"2020-05-30","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778a","source":"朔州日报","title":"新形势下煤矿企业党支部建设的创新思路分析","authors":[""]},{"database":"期刊","times":"2020-05-20","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778b","source":"中国小儿急救医学","title":"儿童急诊和重症医学亚洲网络的成立与发展","authors":["傅昇","钱素云","许峰"]},{"database":"期刊","times":"2020-05-20","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778c","source":"化学分析计量","title":"可移动式微量氧分析仪检定装置研制","authors":["高捷","隋峰","郭波","许峰","仵欣"]},{"database":"期刊","times":"2020-05-20","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778d","source":"中国肺癌杂志","title":"肺结节/肺癌患者全程管理模式的设计与应用","authors":["刘丹","黄燕","周清华","刘伦旭","车国卫"]},{"database":"期刊","times":"2020-05-19","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778e","source":"S形成机理及防治技术","title":"保德煤矿奥陶纪灰岩水H","authors":["张泽源","侯昕悦","王世东","许峰","刘其声"]},{"database":"期刊","times":"2020-05-15","counted":0,"index":"许峰","_id":"5f0bcabbd8a3be14440e778f","source":"煤气与热力","title":"直埋热水供热管道敞沟电预热安装方式","authors":["赵欣刚","王冠英","许峰","王啸","马骏"]},{"database":"期刊","times":"2020-05-15","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7790","source":"中国骨与关节损伤杂志","title":"肘关节镜手术治疗尺骨鹰嘴撞击综合征疗效分析","authors":["张中兴","许峰"]},{"database":"期刊","times":"2020-05-10","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7791","source":"兰州文理学院学报(社会科学版)","title":"文化现代性批评的自觉展开——试论牛学智的文学批评","authors":["许峰"]},{"database":"期刊","times":"2020-05-06","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7792","source":"教育教学论坛","title":"浅论高等数学的发展历史及学习方法","authors":["马文娟","许峰","周继振"]},{"database":"期刊","times":"2020-05-01","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7793","source":"信息记录材料","title":"基于大数据聚类的改进NSGA-Ⅲ算法","authors":["汤恺祥","许峰"]},{"database":"期刊","times":"2020-04-28","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7794","source":"计量学报","title":"恒流放电法测量超级电容器静电容量的影响因素分析与不确定度评定","authors":["许峰","李智玮","冯建"]},{"database":"期刊","times":"2020-04-26","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7795","source":"中国无线电","title":"河北承德无管局助力企业复工复产","authors":[""]},{"database":"期刊","times":"2020-04-25","counted":0,"index":"许峰","_id":"5f0bcabcd8a3be14440e7796","source":"云南畜牧兽医","title":"河南一起2型猪链球菌病的诊断与治疗","authors":["朱文豪","王治方","张青娴","许峰","方剑玉"]},{"database":"期刊","times":"2020-06-30","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e7797","source":"体育科技文献通报","title":"郑开国际马拉松赛发展研究","authors":["张鹏程","肖丹","许萌"]},{"database":"期刊","times":"2020-06-30","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e7798","source":"山西冶金","title":"基于切顶卸压技术的强矿压巷道断顶降压研究","authors":["张鹏程"]},{"database":"期刊","times":"2020-06-25","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e7799","source":"计算机测量与控制","title":"基于大数据的新兴产业计量校准服务平台研究","authors":["张修建","张鹏程"]},{"database":"期刊","times":"2020-06-20","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779a","source":"中南药学","title":"临床药师参与1例术后并发急性弥漫性腹膜炎患者的治疗与分析","authors":["梁海","程刚","张华鹏","张鹏程","陈方"]},{"database":"期刊","times":"2020-06-20","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779b","source":"教育与教学研究","title":"省域学前教育质量监测体系建设研究","authors":["李敏","张鹏程"]},{"database":"报纸","times":"2020-06-16","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779c","source":"中国石油报","title":"全球石油生产者共同应对油市“寒冬”","authors":["中国石油经济技术研究院发展战略所  ","张鹏程"]},{"database":"期刊","times":"2020-06-15","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779d","source":"中国地质调查","title":"太行山中北段神仙山逆冲推覆构造发展与演化","authors":[""]},{"database":"期刊","times":"2020-06-02 10:19","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779e","source":"的电子结构与光催化性质","title":"一种新型二维TiO","authors":["熊子谦","张鹏程","康文斌","方文玉"]},{"database":"期刊","times":"2020-05-31","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e779f","source":"城市勘测","title":"基于三调图斑数据的级联统计模型设计与实现","authors":["吴瑞龙","王明省","何华贵","张鹏程","张珊珊"]},{"database":"期刊","times":"2020-05-28 11:32","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e77a0","source":"测试技术学报","title":"LCC-Demons形变配准算法在自适应放疗中的应用研究","authors":["李汉","王玉","王明泉","张鹏程","阙禄松"]},{"database":"期刊","times":"2020-05-28","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e77a1","source":"湖南中医杂志","title":"中医药治疗慢性肾衰竭的组方用药规律分析","authors":["唐璟","雷蕾","汪之玉","张鹏程","熊维建"]},{"database":"期刊","times":"2020-05-28","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e77a2","source":"地理空间信息","title":"面向公众应用的智慧广州时空云平台设计与实现","authors":["张鹏程","列键佳","杨梅"]},{"database":"期刊","times":"2020-05-26 09:52","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e77a3","source":"物理学报","title":"蛋白质“液-液相分离”的理论和计算方法进展","authors":["张鹏程","方文玉","鲍磊","康文斌"]},{"database":"期刊","times":"2020-05-25","counted":0,"index":"张鹏程","_id":"5f0bcabcd8a3be14440e77a4","source":"牡丹江师范学院学报(自然科学版)","title":"肌内效贴布结合弹力带分级抗阻训练干预大学生颈型颈椎病","authors":["张忠兴","蔡俊","张鹏程"]},{"database":"报纸","times":"2020-05-19","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77a5","source":"中国石油报","title":"全球勘探投入或将进入新一轮下跌周期","authors":["中国石油经济技术研究院发展战略所  ","张鹏程"]},{"database":"期刊","times":"2020-05-15","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77a6","source":"软件导刊","title":"基于LabVIEW的机械臂实时动画显示系统","authors":["田如安","李筠","杨海马","谢孟涛","陈毅"]},{"database":"期刊","times":"2020-05-14","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77a7","source":"卫生软科学","title":"医务社会工作介入社区居家医养结合养老模式的策略研究","authors":["张鹏程","关晓光","洪丹丹","刘杨"]},{"database":"硕士","times":"2020-05-01","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77a8","source":"上海师范大学","title":"基于模因论的偏正复合词的语素教学","authors":["张鹏程"]},{"database":"期刊","times":"2020-04-25","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77a9","source":"水电能源科学","title":"输电线路弯曲导线分层力学特性模型及仿真分析","authors":["祝贺","刘雨菲","张瑾","张鹏程"]},{"database":"期刊","times":"2020-04-25","counted":0,"index":"张鹏程","_id":"5f0bcabdd8a3be14440e77aa","source":"地理信息世界","title":"广东省房屋空置识别及空间特征分析","authors":["箭鸽","张新长","江鑫","王猛","张鹏程"]},{"database":"期刊","times":"2020-06-15","counted":0,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77ab","source":"计算机科学","title":"基于残差生成对抗网络的人脸图像复原","authors":["李泽文","李子铭","费天禄","王瑞琳","谢在鹏"]},{"database":"期刊","times":"2020-05-13 10:16","counted":0,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77ac","source":"计算机科学","title":"基于差分进化的推断任务卸载策略","authors":[""]},{"database":"期刊","times":"2020-05-11 13:21","counted":0,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77ad","source":"计算机工程","title":"一种MapReduce分布式框架下通信负载优化的容错算法","authors":["张基","谢在鹏","毛莺池","徐媛媛","朱晓瑞"]},{"database":"期刊","times":"2020-04-15 14:08","counted":0,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77ae","source":"计算机工程","title":"一种基于分布式编码的同步梯度下降算法","authors":[""]},{"database":"期刊","times":"2018-11-10","counted":0,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77af","source":"核技术","title":"高功率微波系统中弧光效应诊断预警系统设计","authors":["谢在鹏","丁昱凯","梁剑","何源","李永明"]},{"database":"期刊","times":"2018-04-15","counted":13,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77b0","source":"计算机科学","title":"一种基于改进遗传算法的雾计算任务调度策略","authors":["韩奎奎","谢在鹏","吕鑫"]},{"database":"期刊","times":"2018-04-15","counted":2,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77b1","source":"计算机科学","title":"基于稳定匹配的容器部署策略的优化","authors":["施超","谢在鹏","柳晗","吕鑫"]},{"database":"期刊","times":"2004-10-17","counted":6,"index":"谢在鹏","_id":"5f0bcabdd8a3be14440e77b2","source":"电视技术","title":"基于二进小波变换的图像插值方法","authors":["崔海霞","蔡汉添","谢在鹏"]},{"database":"期刊","times":"2020-03-15","counted":0,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b3","source":"计算机与现代化","title":"面向水利信息资源的智能问答系统构建与应用","authors":["张紫璇","陆佳民","姜笑","冯钧"]},{"database":"期刊","times":"2020-02-15","counted":0,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b4","source":"国外电子测量技术","title":"组合式水文模型建模方法综述","authors":["周琦","朱跃龙","陆佳民","冯钧"]},{"database":"期刊","times":"2019-09-09","counted":0,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b5","source":"计算机与现代化","title":"水利信息知识图谱的构建与应用","authors":["冯钧","徐新","陆佳民"]},{"database":"期刊","times":"2018-03-16 17:19","counted":7,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b6","source":"计算机应用","title":"基于HBase的路网移动对象时空索引方法","authors":["冯钧","李顶圣","陆佳民","张立霞"]},{"database":"期刊","times":"2017-11-22 12:16","counted":0,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b7","source":"计算机与现代化","title":"基于Hadoop的空间关键字索引方法","authors":["张进","冯钧","陆佳民"]},{"database":"期刊","times":"2017-10-15","counted":1,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b8","source":"计算机与现代化","title":"面向海量水利数据的索引方法研究","authors":["冯钧","徐维纲","冯读庆","陆佳民","徐欢"]},{"database":"期刊","times":"2017-08-25","counted":10,"index":"陆佳民","_id":"5f0bcabdd8a3be14440e77b9","source":"水利信息化","title":"水利大数据目录服务与资源共享关键技术研究","authors":["陆佳民","冯钧","唐志贤","张鹏程"]},{"database":"期刊","times":"2017-01-20 16:06","counted":3,"index":"陆佳民","_id":"5f0bcabed8a3be14440e77ba","source":"软件学报","title":"路网环境下的移动对象查询技术研究综述","authors":["冯钧","张立霞","陆佳民","王冲"]},{"database":"期刊","times":"2016-10-25","counted":2,"index":"陆佳民","_id":"5f0bcabed8a3be14440e77bb","source":"水利信息化","title":"跨内外网的数据资源整合与共享关键技术研究","authors":["冯钧","佟瑶","陆佳民","沈光泽"]},{"database":"期刊","times":"2015-10-15","counted":1,"index":"陆佳民","_id":"5f0bcabed8a3be14440e77bc","source":"计算机研究与发展","title":"HINMO:基于Hadoop平台的路网移动对象分布式索引结构","authors":["许潇","冯钧","陆佳民","唐志贤","张立霞"]},{"database":"中国会议","times":"2006-11-10","counted":1,"index":"陆佳民","_id":"5f0bcabed8a3be14440e77bd","source":"第二十三届中国数据库学术会议论文集(技术报告篇)","title":"面向城市路网管理的空间索引结构研究","authors":["冯钧","陆佳民","朱跃龙","卢阳"]}],"count":59}

由于本人也是入门选手,写出来的代码难免很难看。希望大家批评指正!

猜你喜欢

转载自blog.csdn.net/qq_36801317/article/details/107408397