我有一个JSON文件,我想从中提取字段。
{
"player": [
{
"Position": "TEST",
"Name": "TEST",
"Squad_No": "TEST",
"Club": "TEST",
"Age": "TEST"
},
{
"Position": "",
"Name": "",
"Squad_No": "",
"Club": "",
"Age": ""
},
]
}
我想取“Name:”字段并将其放入具有以下格式的文件中:
[["TEST",0,0,"","TEST"],["ns1.0a",1,0,"","ns1.0a"],
["ns1.1b",2,0,"","ns1.1b"],["ns1.2",3,0,"","ns1.2"]]
目的是使用Redips加载功能将此文件加载到表中。
有关实现此目标的最佳方法的任何想法?任何帮助将不胜感激。
答案 0 :(得分:1)
使用map
arr = arr.player.map( (s,i) => ( [ s.Name , i, 0, "" , s.Name ] ) );
答案 1 :(得分:0)
阅读文件后你可以像@ gurvinder372那样说:
var fs = require('fs');
var obj;
fs.readFile('file', 'utf8', (err, data) => {
if (err) throw err;
data = JSON.parse(data);
data = data.player.map( (s,i) => ( [ s.Name , i, 0, "" , s.Name ] ) );
console.log(data);
});