我有一个json文件我需要这个文件到我的json代码,我需要在一个对象数组中解析它 这例如是json文件
{
"movies": [
{
"title": "Star Wars",
"year": 1977,
"director": "George Lucas"
},
{
"title": "The Empire Strikes Back",
"year": 1980,
"director": "Irvin Kershner"
},
{
"title": "Return of the Jedi",
"year": 1983,
"director": "Richard Marquand"
}]
}
我希望将其转换为此数组
[{title:"Star Wars", year:1977, director:"George Lucas"},{},{}]
答案 0 :(得分:2)
只需读出电影属性:
var parsedJSON = require('./filename.json');
var result = parsedJSON.movies;
答案 1 :(得分:0)
您可以使用JSON.parse(..)
解析文件字符串:
const fs = require('fs');
var fileString = fs.readFileSync('movies.txt').toString();
var fileObj = JSON.parse(fileString);
var moviesArr = fileObj.movies;
console.log(moviesArr);