[英]How to write JSON object to an existing JSON file
I have a JSON file that I want to update whenever user wants to add a new Object to the file. 我有一个JSON文件,只要用户要将新对象添加到该文件,我都希望更新该文件。 I am using plain HTML and javascript and jquery.
我正在使用纯HTML和javascript和jquery。 Is there a javascript/jquery function/ method that will allow me to be able to append a new JSON object in the external json file?
是否有一个javascript / jquery函数/方法,可以让我在外部json文件中附加一个新的JSON对象?
Below is the structure of my JSON file. 以下是我的JSON文件的结构。
[{
"Links": [{
"url": "https://www.google.com",
"Name": "Google"
}],
"Name": "Google Page"
}, {
"Links": [{
"url": "https://www.facebook.com",
"Name": "Facebook"
}],
"Name": "Facebook Page"
}]
No. 没有。
You would need to read the file, parse the JSON, edit the data structure, serialize back to JSON, then save the file. 您将需要读取文件,解析JSON,编辑数据结构,序列化回JSON,然后保存文件。
JavaScript has no built-in mechanisms for reading or writing files, it depends on the host environment to provide them. JavaScript没有内置的用于读取或写入文件的机制,它依赖于主机环境来提供它们。
Since you tagged this jquery , it seems likely you want to do this in a browser. 由于您已标记此jquery ,因此您似乎可能希望在浏览器中执行此操作。 Browsers have an API for reading files, but not for writing them.
浏览器具有用于读取文件但不用于写入文件的API。 You'll need to use HTTP and write server-side code to handle writing to the file instead.
您将需要使用HTTP并编写服务器端代码来代替处理文件。
You'd probably be best off not using JSON as a storage format (it is great as a transport format), and to run a real database on your server and generate JSON from it on demand. 最好不要使用JSON作为存储格式(它非常适合作为传输格式),而是在服务器上运行真实的数据库并根据需要从中生成JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.