gpt4 book ai didi

javascript - 从多个对象创建有效的 JSON

转载 作者:行者123 更新时间:2023-11-30 21:10:31 25 4
gpt4 key购买 nike

我有下一个代码,它一个一个地生成对象:

...

.data(function(data) {
//data is object: {name: Bruce, surname: Lee}
fs.appendFile('./test.json', JSON.stringify(data, null, 2), function(err) {
console.log(err);
});
})

...

之后,我有了具有以下语法的 test.JSON 文件:

{"name": "Bruce", "surname": "Lee"}{"name": "Bruce", "surname": "Lee"}{"name": "Bruce", "surname": "Lee"}

许多单个对象,JSON 无效...如何在每个对象后添加逗号或使我的 JSON 有效?

预期结果:

{
{"name": "Bruce", "surname": "Lee"},
{"name": "Bruce", "surname": "Lee"},
{"name": "Bruce", "surname": "Lee"}
}

{
"entity": {"name": "Bruce", "surname": "Lee"},
"entity": {"name": "Bruce", "surname": "Lee"},
"entity": {"name": "Bruce", "surname": "Lee"},
}

哪个更有用和正确?

最佳答案

单个实体请求应该如下所示:单个对象

{"name": "Bruce", "surname": "Lee"}

多个实体应该如下所示:多个对象

[
{"name": "Bruce", "surname": "Lee"},
{"name": "Bruce", "surname": "Lee"},
{"name": "Bruce", "surname": "Lee"}
]

关于javascript - 从多个对象创建有效的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46192122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com