Js生成json数据并导出json文件

Js生成json数据并导出json文件

目录

前言

一、生成数据

二、导出json文件

三、完整代码


前言

在实际开发中或者自己学习中经常需要使用到json数据,这时候这个就可以自己

搞定。

一、生成数据

  InitDataFun() {
      let ListData = [];
      let FirstContentData = [];
      let SecondContentData = [];
      let ThirdContentData = [];
      let FourContentData = [];
      for (var i = 0; i < 16; i++) {
        if (i < 6) {
          FirstContentData.push({
            Name: "一级水厂" + (i + 1),
            Label: "一级水厂" + (i + 1),
            Status: 0,
            Msg: [],
            IsFather: true,
            Depth: 1,
            Time: "13:0" + i,
            Id: "Firstid" + (i + 1),
            LID: "Firstlid" + (i + 1),
            List: [
              {
                Title: "浊度",
                Data: 0.23,
              },
              {
                Title: "PH",
                Data: 3,
              },
              {
                Title: "湿度",
                Data: 0.63,
              },
              {
                Title: "COD",
                Data: 0.23,
              },
            ],
          });
          let ListDataSec = [];
          for (var Seci = 0; Seci < 16; Seci++) {
            let ListDatarsd = [];
            for (var Rsdi = 0; Rsdi < 16; Rsdi++) {
              let ListDataFour = [];
              for (var Fouri = 0; Fouri < 16; Fouri++) {
                ListDataFour.push({
                  Name: "四级水厂" + (Fouri + 1),
                  Label: "四级水厂" + (Fouri + 1),
                  Status: 1,
                  Msg: [],
                  IsFather: false,
                  Depth: 4,
                  Time: "13:0" + Fouri,
                  Id: "Fourid" + (Fouri + 1),
                  LID: "Fourlid" + (Fouri + 1),
                  ShowCode:
                    "一级---" +
                    (i + 1) +
                    "---二级---" +
                    (Seci + 1) +
                    "---三级---" +
                    (Rsdi + 1) +
                    "---四级---" +
                    (Fouri + 1),
                  Children: [],
                  List: [
                    {
                      Title: "浊度",
                      Data: 0.23,
                    },
                    {
                      Title: "PH",
                      Data: 3,
                    },
                    {
                      Title: "湿度",
                      Data: 0.63,
                    },
                    {
                      Title: "COD",
                      Data: 0.23,
                    },
                  ],
                });
              }
              ListDatarsd.push({
                Name: "三级水厂" + (Rsdi + 1),
                Label: "三级水厂" + (Rsdi + 1),
                Status: 1,
                Msg: [],
                IsFather: true,
                Depth: 3,
                Time: "13:0" + Rsdi,
                Id: "Thirdid" + (Rsdi + 1),
                LID: "Thirdlid" + (Rsdi + 1),
                ShowCode:
                  "一级---" +
                  (i + 1) +
                  "---二级---" +
                  (Seci + 1) +
                  "---三级---" +
                  (Rsdi + 1),
                Children: ListDataFour,
                List: [
                  {
                    Title: "浊度",
                    Data: 0.23,
                  },
                  {
                    Title: "PH",
                    Data: 3,
                  },
                  {
                    Title: "湿度",
                    Data: 0.63,
                  },
                  {
                    Title: "COD",
                    Data: 0.23,
                  },
                ],
              });
            }
            ListDataSec.push({
              Name: "二级水厂" + (Seci + 1),
              Label: "二级水厂" + (Seci + 1),
              Status: 0,
              Msg: [],
              IsFather: true,
              Depth: 2,
              Time: "13:0" + Seci,
              Id: "Secondid" + (Seci + 1),
              LID: "Secondlid" + (Seci + 1),
              ShowCode: "一级---" + (i + 1) + "---二级---" + (Seci + 1),
              Children: ListDatarsd,
              List: [
                {
                  Title: "浊度",
                  Data: 0.23,
                },
                {
                  Title: "PH",
                  Data: 3,
                },
                {
                  Title: "湿度",
                  Data: 0.63,
                },
                {
                  Title: "COD",
                  Data: 0.23,
                },
              ],
            });
          }
          ListData.push({
            Name: "一级水厂" + (i + 1),
            Label: "一级水厂" + (i + 1),
            Status: 0,
            Msg: [],
            IsFather: true,
            Depth: 1,
            Time: "13:0" + i,
            Id: "Firstid" + (i + 1),
            LID: "Firstlid" + (i + 1),
            ShowCode: "一级---" + (i + 1),
            List: [
              {
                Title: "浊度",
                Data: 0.23,
              },
              {
                Title: "PH",
                Data: 3,
              },
              {
                Title: "湿度",
                Data: 0.63,
              },
              {
                Title: "COD",
                Data: 0.23,
              },
            ],
            Children: ListDataSec,
          });
        }
        SecondContentData.push({
          Name: "二级水厂" + (i + 1),
          Label: "二级水厂" + (i + 1),
          Status: 0,
          Msg: [],
          IsFather: true,
          Depth: 2,
          Time: "13:0" + i,
          Id: "Secondid" + (i + 1),
          LID: "Secondlid" + (i + 1),
          List: [
            {
              Title: "浊度",
              Data: 0.23,
            },
            {
              Title: "PH",
              Data: 3,
            },
            {
              Title: "湿度",
              Data: 0.63,
            },
            {
              Title: "COD",
              Data: 0.23,
            },
          ],
        });
        ThirdContentData.push({
          Name: "三级水厂" + (i + 1),
          Label: "三级水厂" + (i + 1),
          Status: 1,
          Msg: [],
          IsFather: true,
          Depth: 3,
          Time: "13:0" + i,
          Id: "Thirdid" + (i + 1),
          LID: "Thirdlid" + (i + 1),
          List: [
            {
              Title: "浊度",
              Data: 0.23,
            },
            {
              Title: "PH",
              Data: 3,
            },
            {
              Title: "湿度",
              Data: 0.63,
            },
            {
              Title: "COD",
              Data: 0.23,
            },
          ],
        });
        FourContentData.push({
          Name: "四级水厂" + (i + 1),
          Label: "四级水厂" + (i + 1),
          Status: 1,
          Msg: [],
          IsFather: false,
          Depth: 4,
          Time: "13:0" + i,
          Id: "Fourid" + (i + 1),
          LID: "Fourlid" + (i + 1),
          List: [
            {
              Title: "浊度",
              Data: 0.23,
            },
            {
              Title: "PH",
              Data: 3,
            },
            {
              Title: "湿度",
              Data: 0.63,
            },
            {
              Title: "COD",
              Data: 0.23,
            },
          ],
        });
      }
      this.FirstContentData = FirstContentData;
      this.SecondContentData = SecondContentData;
      this.ThirdContentData = ThirdContentData;
      this.FourContentData = FourContentData;
      this.ListData = SecondContentData;
      this.ShowListData = JSON.stringify(SecondContentData);
      // console.log("ListData",ListData)
      //console.log("JSON.stringify(ListData)", JSON.stringify(ListData));
    },

二、导出json文件

  saveJSON(data, filename) {
      if (!data) {
        alert("保存的数据为空");
        return;
      }
      if (!filename) filename = "json.json";
      if (typeof data === "object") {
        data = JSON.stringify(data, undefined, 4);
      }
      var blob = new Blob([data], { type: "text/json" }),
        e = document.createEvent("MouseEvents"),
        a = document.createElement("a");
      a.download = filename;
      a.href = window.URL.createObjectURL(blob);
      a.dataset.downloadurl = ["text/json", a.download, a.href].join(":");
      e.initMouseEvent(
        "click",
        true,
        false,
        window,
        0,
        0,
        0,
        0,
        0,
        false,
        false,
        false,
        false,
        0,
        null
      );
      a.dispatchEvent(e);
    },

三、完整代码







你可能感兴趣的:(js,vue.js,javascript,前端,前端框架,json)