gpt4 book ai didi

jquery - 从 JQuery 将 JSON 写入文本文件

转载 作者:行者123 更新时间:2023-12-01 02:07:04 25 4
gpt4 key购买 nike

我有一堆 JSON 对象存储在 JQuery 的数组中。有没有一种简单的方法可以将它们重写为 JSON 文本文件到我的本地计算机?

谢谢!

最佳答案

您可以使用数据 URI 方案将 JSON 转换为可以下载的“URL”,即

/* Firstly you'll need a javascript library which can encode data to base 64
* http://archive.plugins.jquery.com/project/base64 - jQuery Plugin
* http://www.webtoolkit.info/javascript-base64.html - Collection of functions
* http://stackoverflow.com/a/6740027/451672 - '' '' "
*/

/* Now we must convert the JSON to text, which can be done with the
JSON.stringify() method
* http://msdn.microsoft.com/en-us/library/cc836459(v=vs.85).aspx
*/

var data = JSON.stringify(myObject);

/* Now we convert the data to a Data URI Scheme, which must be Base64 encoded
make sure you use the appropriate method to Base64 encode your data depending
on the library you chose to use.
* application/octet-stream simply tells your browser to treat the URL as
arbitrary binary data, and won't try to display it in the browser window when
opened.
*/
var url = "data:application/octet-stream;base64," + Base64.encode(data);

/* To force the browser to download a file we need to use a custom method which
creates a hidden iframe, which allows browsers to download any given file
* http://stackoverflow.com/a/3749395/451672
*/
var downloadURL = function(url)
{
var iframe;
iframe = document.getElementById("hiddenDownloader");
if (iframe === null)
{
iframe = document.createElement('iframe');
iframe.id = "hiddenDownloader";
iframe.style.display = "none";
document.body.appendChild(iframe);
}
iframe.src = url;
}

/* Now downloading is simple as executing the following code, make sure that
the DOM is not modified until after the page has finished loading!
*/
window.onload = function()
{
var link = document.getElementById("downloadJSON");
link.onclick = function()
{
downloadURL(url);
}
}

jsFiddle: http://jsfiddle.net/cQV7X/

关于jquery - 从 JQuery 将 JSON 写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10150824/

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