从节点js中的本地Json文件中获取值和键

时间:2017-12-11 08:21:09

标签: javascript arrays json node.js

Hello Everyone,我无法从本地json文件中获取项目作为NodeJs中的值和键。

[
{
"question":"What is the first name of your best friend in high school?",
"answer":"U2FsdGVkX18/l/UF1DbqMp9ItOqgmmQIeVUlxqrb7vE="},
{
"question":"What was your childhood nickname?",
"answer":"U2FsdGVkX1+x14bgpnjr3UcvScBDbIOnP55vua2NvME="},
{
"question":"What is the name of the street you grew up in?",
"answer":"U2FsdGVkX1+pGkO/MRIP7oHym3Ynz/7n/fYuEt7fTFw="}]

这是本地json文件。 我如何获取问题和答案并将其保存到不同的阵列。 提前致谢。

3 个答案:

答案 0 :(得分:1)

var data = [
{
    "question":"What is the first name of your best friend in high school?",
    "answer":"U2FsdGVkX18/l/UF1DbqMp9ItOqgmmQIeVUlxqrb7vE="},
{
    "question":"What was your childhood nickname?",
    "answer":"U2FsdGVkX1+x14bgpnjr3UcvScBDbIOnP55vua2NvME="},
{
    "question":"What is the name of the street you grew up in?",
    "answer":"U2FsdGVkX1+pGkO/MRIP7oHym3Ynz/7n/fYuEt7fTFw="
}];

var data = JSON.parse(data); 
var questions = data.map(current =>  current.question);
var answers = data.map(current => current.answer);

答案 1 :(得分:1)

假设您的本地数据文件位于data.json,您可以使用它,如下所示。

var data = require('./data.json')
console.log(data[0]);

答案 2 :(得分:0)

如果你想从文件系统中读取你的json文件,那就是:

$scope.requestedLeaveTypeCount=[2,2,2,2]