gpt4 book ai didi

ruby - 将 JSON 字符串 append 到 Ruby 中已包含 JSON 的文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:50 27 4
gpt4 key购买 nike

我正在尝试将一个 JSON 字符串 append 到一个已经有更多 JSON 字符串的文件中:

[
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnMiP"
},
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnZiP"
},
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnAiP"
}
]

我需要 append 像 {"title":"test","content":"test","date":"2011-12-10 21:35:48 +0100"这样的字符串"author":"zad0xsis","id":"0EhQ0"} 保留 JSON 格式:

[
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnMiP"
},
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnZiP"
},
{
"title": "A test title",
"content": "A test content",
"date": "10/01/10",
"author": "zad0xsis",
"id": "vnAiP"
},
{"title":"test","content":"test","date":"2011-12-10 21:35:48 +0100","author":"zad0xsis","id":"0EhQ0"}
]

所以它看起来像这样,简而言之, append 它以便它仍然是有效的 JSON。我怎样才能在 Ruby 中实现这一点?谢谢!

最佳答案

使用 json gem,您可以解析文件的内容,将新哈希添加到数组并将其重新编码为 JSON 以重写文件。

关于ruby - 将 JSON 字符串 append 到 Ruby 中已包含 JSON 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8459711/

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