参考 saveJSON()

saveJSON()

Saves an Object or Array to a JSON file.

JavaScript Object Notation (JSON) is a standard format for sending data between applications. The format is based on JavaScript objects which have keys and values. JSON files store data in an object with strings as keys. Values can be strings, numbers, Booleans, arrays, null, or other objects.

The first parameter, json, is the data to save. The data can be an array, as in [1, 2, 3], or an object, as in { x: 50, y: 50, color: 'deeppink' }.

The second parameter, filename, is a string that sets the file's name. For example, calling saveJSON([1, 2, 3], 'data.json') saves the array [1, 2, 3] to a file called data.json on the user's computer.

The third parameter, optimize, is optional. If true is passed, as in saveJSON([1, 2, 3], 'data.json', true), then all unneeded whitespace will be removed to reduce the file size.

Note: The browser will either save the file immediately or prompt the user with a dialogue window.

示例

语法

saveJSON(json, filename, [optimize])

参数

json
Array|Object:

data to save.

filename
String:

name of the file to be saved.

optimize
Boolean:

whether to trim unneeded whitespace. Defaults to true.

Notice any errors or typos? Please let us know. Please feel free to edit src/io/files.js and open a pull request!

相关参考