如何使用打字稿在 json 文件中附加数据?
How to append data in json file using typescript?
我在 .json 文件中附加数据时遇到问题。所以我搜索了这个问题,找到了很多结果,但对我不起作用。如果有一个库可以方便地在 json 文件中附加数据或修复我的代码,我将不胜感激
这是我的函数
function writeToJson(path: string, data: any, rewrite: boolean = false)
{
let old_data: any = fs.readFileSync(path)
if(old_data.length == 0 || rewrite == true)
{
fs.writeFileSync(path, JSON.stringify(data, null, 4))
return
}
let json_obj: any = [JSON.parse(old_data)] // without brackets it reverts an error
json_obj.push(data)
fs.writeFileSync(path, JSON.stringify(json_obj, null, 4))
}
下面是实际结果。json
[
[
[
[
[
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:44 AM"
},
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:53 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:55 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:58 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:01 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
]
因此我希望得到类似
的结果
{
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
...
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
}
您可能只是错过了指定编码:
fs.readFileSync(path, "utf8")
Node.js doc of fs.readFileSync
:
If the encoding
option is specified then this function returns a string. Otherwise it returns a buffer.
所以现在您应该可以按预期使用它了:
interface Data {
price: number;
timestamp: string;
}
let old_data: string = fs.readFileSync(path, "utf8")
// ...
let json_obj: Data[] = JSON.parse(old_data) || [] // No longer need extra array; initialoze as an empty array if needed
json_obj.push(data)
问题出在 .json 文件中,而不是函数中。所以如果你想让你的脚本正常工作
function writeToJson(path: string, data: any, rewrite: boolean = false)
{
let old_data: any = fs.readFileSync(path)
if(old_data.length == 0 || rewrite == true)
{
fs.writeFileSync(path, JSON.stringify(data, null, 4))
return
}
let json_obj: any = [JSON.parse(old_data)] // without brackets it reverts an error
json_obj.push(data)
fs.writeFileSync(path, JSON.stringify(json_obj, null, 4))
}
确保您的 json 文件已经以方括号开头
[
// empty or some data
]
我在 .json 文件中附加数据时遇到问题。所以我搜索了这个问题,找到了很多结果,但对我不起作用。如果有一个库可以方便地在 json 文件中附加数据或修复我的代码,我将不胜感激
这是我的函数
function writeToJson(path: string, data: any, rewrite: boolean = false)
{
let old_data: any = fs.readFileSync(path)
if(old_data.length == 0 || rewrite == true)
{
fs.writeFileSync(path, JSON.stringify(data, null, 4))
return
}
let json_obj: any = [JSON.parse(old_data)] // without brackets it reverts an error
json_obj.push(data)
fs.writeFileSync(path, JSON.stringify(json_obj, null, 4))
}
下面是实际结果。json
[
[
[
[
[
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:44 AM"
},
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:53 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:55 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:06:58 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:01 AM"
}
],
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
]
因此我希望得到类似
的结果{
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
...
{
"price": 0.5631746759368121,
"timestamp": "5/4/2022, 2:07:04 AM"
}
}
您可能只是错过了指定编码:
fs.readFileSync(path, "utf8")
Node.js doc of fs.readFileSync
:
If the
encoding
option is specified then this function returns a string. Otherwise it returns a buffer.
所以现在您应该可以按预期使用它了:
interface Data {
price: number;
timestamp: string;
}
let old_data: string = fs.readFileSync(path, "utf8")
// ...
let json_obj: Data[] = JSON.parse(old_data) || [] // No longer need extra array; initialoze as an empty array if needed
json_obj.push(data)
问题出在 .json 文件中,而不是函数中。所以如果你想让你的脚本正常工作
function writeToJson(path: string, data: any, rewrite: boolean = false)
{
let old_data: any = fs.readFileSync(path)
if(old_data.length == 0 || rewrite == true)
{
fs.writeFileSync(path, JSON.stringify(data, null, 4))
return
}
let json_obj: any = [JSON.parse(old_data)] // without brackets it reverts an error
json_obj.push(data)
fs.writeFileSync(path, JSON.stringify(json_obj, null, 4))
}
确保您的 json 文件已经以方括号开头
[
// empty or some data
]