![中级用户,我必须修改 json 文件以合并新值,请帮忙?](https://linux22.com/image/940056/%E4%B8%AD%E7%BA%A7%E7%94%A8%E6%88%B7%EF%BC%8C%E6%88%91%E5%BF%85%E9%A1%BB%E4%BF%AE%E6%94%B9%20json%20%E6%96%87%E4%BB%B6%E4%BB%A5%E5%90%88%E5%B9%B6%E6%96%B0%E5%80%BC%EF%BC%8C%E8%AF%B7%E5%B8%AE%E5%BF%99%EF%BC%9F.png)
这是我必须修改的代码,我已将文件复制到终端中,现在需要将我自己的 ID 等放入代码中。我使用 Cat 看到了此代码,但不知道如何编辑它。任何帮助都非常感谢,因为我正在使用它来演示睡眠统计数据在 Facebook 上的工作原理。
{
"fbCookie": {
"c_user": "",
"xs": ""
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}
{
"fbCookie": {
"c_user": "",
"xs": ""
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}
答案1
您可以使用杰奇从命令行操作 JSON 的工具,例如
$ jq --arg u "myuser" '.fbCookie.c_user |= $u' file.json
{
"fbCookie": {
"c_user": "myuser",
"xs": ""
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}
{
"fbCookie": {
"c_user": "myuser",
"xs": ""
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}
或者
$ jq --argjson c '{"c_user": "myuser", "xs": "foo bar"}' '.fbCookie |= $c' file.json
{
"fbCookie": {
"c_user": "myuser",
"xs": "foo bar"
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}
{
"fbCookie": {
"c_user": "myuser",
"xs": "foo bar"
},
"pollingInterval": 600,
"appId": 435522656639081,
"server": {
"port": 3000
}
}