• Servlet中生成json文件,echarts模板调用


    在echarts官网中下载的模板数据都是固定的,而我的项目需要数据是动态改变的,所以我试了很多方法来解决这个问题,其中成功的一种方法是在Servlet中先生成json数据格式,之后再把内容写到一个json文件中,在HTML中在调用这个json文件就可以了。

    先生成json数据格式

    JSONObject q1 = new JSONObject();
            JSONObject q2 = new JSONObject();
            JSONObject q3 = new JSONObject();
            JSONObject q4 = new JSONObject();
            JSONArray array = new JSONArray();
            JSONArray array2 = new JSONArray();
            JSONArray array3 = new JSONArray();
            JSONObject w1 = new JSONObject();
            JSONObject w2 = new JSONObject();
            JSONObject s = new JSONObject();
            JSONArray htags = new JSONArray();
            for(int i=0;i<beank.size();i++)
            {
                q1=new JSONObject();
                q1.put("name", beank.get(i).getDISTNAME());
                System.out.println(beank.get(i).getDISTNAME());
                array.add(q1);
            }
            for(int i=0;i<beanl.size();i++)
            {
                q1=new JSONObject();
                q1.put("name", beanl.get(i).getSTOCKNAME());
                System.out.println(beanl.get(i).getSTOCKNAME());
                array2.add(q1);
            }
            w1.put("name", "对外投资");
            w1.put("children", array);
            
            w2.put("name", "股东");
            w2.put("children", array2);
            array3.add(w1);
            array3.add(w2);
            s.put("children", array3);
            s.put("name", corpbean.getCORPNAME());

    在规定写入的文件并写入

    Tool tool=new Tool();
            File file=new File("F:\web\Company\WebContent\data\Text.json");
            if(!file.exists())//判断文件是否存在,若不存在则新建
            {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream=new FileOutputStream(file);//实例化FileOutputStream
            OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");//将字符流转换为字节流
            BufferedWriter bufferedWriter= new BufferedWriter(outputStreamWriter);//创建字符缓冲输出流对象
            String jsonString=s.toString();//将jsonarray数组转化为字符串
            bufferedWriter.write(jsonString);//将格式化的jsonarray字符串写入文件
            bufferedWriter.flush();//清空缓冲区,强制输出数据
            bufferedWriter.close();//关闭输出流

    之后就可以使用了

    运行结果:

  • 相关阅读:
    html meta标签
    随滚动条滚动,动态修改元素class
    获取浏览器长宽自动设置
    SpringMVC常用注解實例詳解2:@ModelAttribute
    SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
    Freemarker常用指令使用范例
    Spring整合Freemarker
    SpringMVC配置入門
    再谈深浅拷贝 后端
    转发-react 性能深度探讨
  • 原文地址:https://www.cnblogs.com/liujinxin123/p/11904322.html
Copyright © 2020-2023  润新知