简体   繁体   中英

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. I am using plain HTML and javascript and 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?

Below is the structure of my JSON file.

[{
  "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.

JavaScript has no built-in mechanisms for reading or writing files, it depends on the host environment to provide them.

Since you tagged this , it seems likely you want to do this in a browser. Browsers have an API for reading files, but not for writing them. You'll need to use HTTP and write server-side code to handle writing to the file instead.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM