我正在将对象数组写入json文件
fs.writeFileSync('fixturesString.json', JSON.stringify(dateFixtures,null,4),'utf8');
它将以以下格式写入文件:
[
{
"season": "2018-19",
"competition": "Premier League",
"matchDay": "2018-12-08",
"homeTeam": "Bournemouth",
"awayTeam": "Liverpool"
},
{
"season": "2018-19",
"competition": "Premier League",
"matchDay": "2018-12-08",
"homeTeam": "Arsenal",
"awayTeam": "Huddersfield"
},
{
"season": "2018-19",
"competition": "Premier League",
"matchDay": "2018-12-08",
"homeTeam": "Burnley",
"awayTeam": "Brighton"
}
]
有没有一种可以使用的方法,以便每个对象都在自己的行上写入文件,例如。
{"season":"2018-19","competition":"Premier League","matchDay":"2018-12-08","homeTeam":"Bournemouth","awayTeam":"Liverpool"},
{"season":"2018-19","competition":"Premier League","matchDay":"2018-12-08","homeTeam":"Arsenal","awayTeam":"Huddersfield"}
非常感谢您的帮助
Mos。
答案 0 :(得分:0)
您的案例没有标准功能,但是如果您需要将此特殊案例设置为逐行模式,则可以简单地使用字符串替换,例如
fs.writeFileSync('a.json', JSON.stringify(d).replace(',{', ',\n{'),'utf8');
如果您使用的是Windows,请使用\r\n
代替\n
Ofc不适用于一般情况,但是方法很明确。