saveJSON()函數用於將對象或對象數組作為JSON對象寫入到.json
文件。文件的保存將因網絡瀏覽器而異。
用法:
saveJSON( json, filename, optimize )
參數:此函數接受上述和以下所述的三個參數:
- json:它是一個對象或對象數組,將形成要創建的JSON對象的內容。
- filename:它指定用作已保存文件的文件名的字符串。
- optimize:這是一個布爾值,它指定在寫入JSON對象之前是否將其換行符和空格刪除。它是一個可選參數。
以下示例說明了p5.js中的saveJSON()函數:
範例1:
function setup() {
createCanvas(600, 300);
textSize(22);
text("Click on the button below to "
+ "save the JSON Object", 20, 20);
bookObj = {};
bookObj.name = "Let US C";
bookObj.author = "Yashavant Kanetkar";
bookObj.price = "120";
// Create a button for saving the JSON Object
saveBtn = createButton("Save JSON object to file");
saveBtn.position(30, 50)
saveBtn.mousePressed(saveFile);
}
function saveFile() {
// Save the JSON object to file
saveJSON(bookObj, 'books.json', true);
}
輸出:
範例2:
function setup() {
createCanvas(600, 300);
textSize(22);
text("Click on the button below to "
+ "save the JSON Array", 20, 20);
bookArray = [];
for (let i = 1; i <= 3; i++) {
bookObj = {};
bookObj.name = "Book " + i;
bookObj.author = "Author " + i;
bookArray.push(bookObj);
}
// Create a button for saving JSON Object
saveBtn = createButton("Save JSON Array to file");
saveBtn.position(30, 50)
saveBtn.mousePressed(saveFile);
}
function saveFile() {
// Save the JSON object to file
saveJSON(bookArray, 'books-list.json');
}
輸出:
在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5/saveJSON
相關用法
- PHP pos()用法及代碼示例
- PHP Ds\Set contains()用法及代碼示例
- p5.js hue()用法及代碼示例
- PHP abs()用法及代碼示例
- p5.js pan()用法及代碼示例
- PHP max( )用法及代碼示例
- d3.js d3.hsl()用法及代碼示例
- PHP key()用法及代碼示例
- d3.js d3.map.has()用法及代碼示例
- PHP min( )用法及代碼示例
- PHP next()用法及代碼示例
- p5.js hex()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | saveJSON() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。