当前位置:  开发笔记 > 编程语言 > 正文

解析Json(包含数组和对象)并将数据导出到Node.js中的Excel文件中

如何解决《解析Json(包含数组和对象)并将数据导出到Node.js中的Excel文件中》经验,为你挑选了1个好方法。

我是Node.js的新手.我的要求是,我需要解析JSON并将数据导出到包含JSON中所有字段的Excel文件中.

我的JSON如下:

{
  "id": 1255,
  "title": "The Brain and Nervous System (LS1.D)",
  "description": "By the time you finish this playlist, you should be able to: 1. Describe how the nervous system is organized and how it works 2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain",
  "keyTerms": "You should also be able to define the following words: stimulus, response, neuron, sensory neuron, motor neuron, nerve impulse, dendrite, axon, nerve, central nervous system, peripheral nervous system, brain, spinal cord, cerebrum, cerebellum, brain stem, retina, cochlea",
  "visible": true,
  "introduction": {
    "id": 5336,
    "title": "Introductory Materials",
    "resources": [
      {
        "id": 23022,
        "title": "Vocabulary - Brain and Nervous System",
        "description": "",
        "purpose": "",
        "category": "Website",
        "position": 1,
        "contentItem": {
          "id": 1650,
          "url": "http://quizlet.com/45497180/flashcards",
          "itemType": "Website",
          "embedUrl": null
        }
      },
      {
        "id": 23023,
        "title": "The Brain and Nervous System Study Guide",
        "description": "Fill out this study guide while studying! It will help you prepare for the assessment!",
        "purpose": "",
        "category": "Website",
        "position": 2,
        "contentItem": {
          "id": 12581,
          "url": "https://docs.google.com/a/summitps.org/document/d/1TjF1MY3cyGNKT4s46uk1iz5NvjrY59eNPH8YKYYTC_E/edit",
          "itemType": "Website",
          "embedUrl": null
        }
      }
    ]
  },
  "objectives": [
    {
      "id": 10732,
      "title": "1. Describe how the nervous system is organized",
      "caContribution": 5,
      "position": 1,
      "resources": [
        {
          "id": 23024,
          "title": "Reading - How the Nervous System Works",
          "description": "",
          "purpose": "",
          "category": "Document",
          "position": 1,
          "contentItem": {
            "id": 1651,
            "url": null,
            "itemType": "Document",
            "embedUrl": "https://view-api.box.com/1/sessions/493fca96d46a4559813c3118ebeef8b6/view?theme=light",
            "s3Url": "/files/content_items/relateds/000/001/651/original/53d1ddd8f07787731aa7d84f-how_20nervous_20system_20works_001.pdf?1424368501"
          }
        }
      ]
    },
    {
      "id": 10734,
      "title": "2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain",
      "caContribution": 5,
      "position": 2,
      "resources": [
        {
          "id": 23039,
          "title": "Study Jams - The Senses",
          "description": "This series of videos explains the different senses: sight, hearing, taste, touch, and smell",
          "purpose": "",
          "category": "Website",
          "position": 1,
          "contentItem": {
            "id": 1666,
            "url": "http://studyjams.scholastic.com/studyjams/jams/science/human-body/touching.htm",
            "itemType": "Website",
            "embedUrl": null
          }
        },
        {
          "id": 23040,
          "title": "Nervous System: I'm Sensing Something",
          "description": "Format: Article Content: How does the nervous system work and interact with other systems",
          "purpose": "",
          "category": "Website",
          "position": 1,
          "contentItem": {
            "id": 12582,
            "url": "http://www.biology4kids.com/files/systems_nervous.html",
            "itemType": "Website",
            "embedUrl": null
          }
        }
      ]
    }
  ]
}

我使用了json2xls模块,我得到了电子表格,其中包含字段id,title,description,keyTerms,visible,introduction,objective,其中介绍和目标来自[object] [object],[object] [object]

这是导出到excel文件的代码:

var fs = require("fs");
var json2xls = require("json2xls");

fs.readFile('/home/e100093/nodejs/sampleJson.json','utf8',function(err,body){

var jsonData = JSON.parse(body);

var xls = json2xls(jsonData);
fs.writeFileSync('final-test.xlsx', xls, 'binary');

});

但我想要输出字段,如id,title,description,keyTerms,visible,introduction.id,introduction.title,introduction.resources.id,introduction.resources.title,...,objectives.id,objectives.title,goals .description,.....等.

我需要动态地解析对象和数组,并在电子表格的每一列中生成字段.

包含列和数据的示例输出如下:

id  title  description  keyTerms  visible  introduction.id  introduction.title  introduction.resources.id  introduction.resources.title  introduction.resources.description  introduction.resources.purpose  introduction.resources.category  introduction.resources.position  introduction.resources.contentItem.id  introduction.resources.contentItem.url  introduction.resources.contentItem.itemType  introduction.resources.contentItem.embedUrl  objectives.id  objectives.title  objectives.caContribution  objectives.position  objectives.resources.id  objectives.resources.title  objectives.resources.description  objectives.resources.purpose  objectives.resources.category  objectives.resources.position  objectives.resources.contentItem.id  objectives.resources.contentItem.url  objectives.resources.contentItem.itemType  objectives.resources.contentItem.embedUrl  objectives.resources.contentItem.s3Url

1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  5336  Introductory Materials  23022  Vocabulary - Brain and Nervous System  null  null  Website  1  1650  http://quizlet.com/...  Website  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  5336  Introductory Materials  23023  The Brain and Nervous System Study Guide  Fill out this...  null  Website  2  12581  https://docs.google.com/...  Website  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10732  1. Describe how...  5  1  23024  Reading - How...  null  null  Document  1  1651  null  Document  https://view-api.box.com/1...  /files/content...  
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10734  2. Describe the role...  5  2  23039  Study Jams - The Senses  This series of videos  null  Website  1  1666  http://studyjams.scholastic.com...  Website  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10734  2. Describe the role...  5  2  23040  Nervous System: I'm Sensing... Format: Article Content...  null  Website  1  12582  http://www.biology4kids.com...  Website  null

请帮我怎么做.



1> agershun..:

您可以使用具有特殊运算符的AlaSQL javascript库来执行此SEARCH操作,该运算符旨在搜索嵌套对象.请参阅下面的代码,它会生成包含您的结构的Excel文件(我没有包含所有字段):

var alasql = require('alasql');

var data = [{
   "id": 1255,
   "title": "The Brain and Nervous System (LS1.D)",
   // ...
},
{
   "id": 1256,
    // ...
}];

// Here is search query
alasql('SEARCH / AS @a \
    UNION ALL( \
      introduction AS @b \
      resources / AS @c \
      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \
        @a->keyTerms AS keyTerms, @a->visible AS visible, \
        @b->id as [introduction.id], @b->title as [introduction.title], \
        @c->id AS [introduction.resources.id], \
        @c->contentItem->id AS [introduction.resources.contentItem.id] \
      ) \
    , \
      objectives AS @b \
      resources / AS @c \
      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \
        @a->keyTerms AS keyTerms, @a->visible AS visible, \
        @b->id as [objectives.id], @b->title as [objectives.title], \
        @c->id AS [objectives.resources.id], \
        @c->contentItem->id AS [objectives.resources.contentItem.id] \
      ) \
    ) INTO XLSX("test411.xlsx",{headers:true})\
    FROM ?',[data]);

一些解释:

SEARCH - 是查询嵌套对象的特殊语句

/ - 循环数组元素

UNION ALL(...,...) - 所有嵌套查找记录的并集

AS @variable - 将当前搜索位置保存为临时变量

介绍 - 深入了解物业"介绍"

RETURN(...,...) - 创建一个包含属性的记录(JSON对象)

RETURN(值AS别名) - 值的别名

@ a-> id - 获取类似于JavaScript中的a.id的id属性

[...] - 与任何特殊字符的条款

INTO XLSX("test411.xlsx",{headers:true}) - 使用标题将结果保存到Excel文件

从? - 从参数中获取数据

alasql(sql,[data]) - 将数据变量作为查询的第一个参数

您还需要添加其他列以完成此查询.样本文件将在两小时内上传到此处.

您还可以从源代码中删除此行:INTO XLSX(),alasql()并将返回包含所有属性的JSON对象:

var res = alasql('SEARCH / AS @a UNION ALL(...) FROM ?',[data]);
console.log(res);

这是jsFiddle的例子

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有