如何将 Javascript 对象保存到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12597364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a Javascript object to file ?
提问by János Továbbító
I need to save a complex javascript object to a file for later investigation. It is a very big object, more then 50 methods and propeties.
我需要将一个复杂的 javascript 对象保存到一个文件中以供以后调查。这是一个非常大的对象,有超过 50 个方法和属性。
I can see the object and its methods and properties (and its values) in Firefox-Firebug on DOM page, but i can't save it to a file from there.
我可以在 DOM 页面上的 Firefox-Firebug 中看到对象及其方法和属性(及其值),但我无法从那里将其保存到文件中。
I want save the object with current values of properties, not the HTML doc. Any format of a file - HTML or JSON or anything else is good for me :)
我想用当前的属性值保存对象,而不是 HTML 文档。任何格式的文件 - HTML 或 JSON 或其他任何格式对我都有好处:)
How can I save the object?
如何保存对象?
回答by alexandernst
Well... There is something you can do, but I can't say how ugly is. You can do something like
嗯......你可以做一些事情,但我不能说它有多丑。你可以做类似的事情
JSON.stringify(my_big_javascript_object)
and then save the resulting JSON (plain text) in a file.
JSON.stringify(my_big_javascript_object)
然后将生成的 JSON(纯文本)保存在一个文件中。
You can look at the values later using some JSON viewer, like http://jsonviewer.stack.hu/
您可以稍后使用一些 JSON 查看器查看这些值,例如http://jsonviewer.stack.hu/
回答by kashiraja
It is possible now using the Blob API:
现在可以使用 Blob API:
function downloadObject(obj, filename){
var blob = new Blob([JSON.stringify(obj, null, 2)], {type: "application/json;charset=utf-8"}).slice(2,-1);
var url = URL.createObjectURL(blob);
var elem = document.createElement("a");
elem.href = url;
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/API/Blob
您可以在此处阅读更多相关信息:https: //developer.mozilla.org/en-US/docs/Web/API/Blob
It requires that you have a webpage up with a live DOM, which should work if you are in the middle of debugging javascript on a page.
它要求您有一个带有实时 DOM 的网页,如果您正在调试页面上的 javascript,它应该可以工作。
回答by PolyGlot
This works well for me.
这对我很有效。
//Initialization of object to save
var objectToSave={first:'string', second: function(){}};
//Start of saving method
var textToSave='';//String to be saved
var count=0;
for(var i in objectToSave){
//Adding every key name + type + text value to string
textToSave+=objectToSave[i].constructor.name+' '+ Object.keys(objectToSave)[count]+' = '+objectToSave[i]+'\n';
count++;
}
//Saving string to file using html clicking trick
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.txt';
hiddenElement.click();
Result of this method is saved txt file with text:
此方法的结果是保存带有文本的 txt 文件:
String first = string
Function second = function (){}
字符串优先 = 字符串
函数第二个 = 函数 (){}
回答by bogatyrjov
You can pass the object from Your page to server-side script using AJAX like this (jQuery):
您可以像这样使用 AJAX (jQuery) 将对象从您的页面传递到服务器端脚本:
var ajax_object // Your object
.ajax({
type: "POST",
url: "tofile.php",
data: ajax_object,
});
and then write it down to a HTML file using the server-side script (example is using PHP):
然后使用服务器端脚本将其写入 HTML 文件(例如使用 PHP):
// File: tofile.php
$ajax_object // Object, which You have passed using AJAX
ob_start();
print_r("<pre>".print_r($ajax_object, true)."</pre>");
$var = ob_get_contents();
ob_end_clean();
$fp = fopen('somefile.htm', 'w');
fputs($fp, $var);
fclose($fp);
The output in somefile.htm would be similar to this:
somefile.htm 中的输出类似于:
Some_Object Object
(
[some_element] => 123
[some_array] => Array
(
[element1] => 456
[element2] => 789
[element3] => 012
)
)
If You are wondering how to save Your object to a file using only Javascript, without server-side language, then, I am afraid, it is not possible.
如果您想知道如何仅使用 Javascript 将您的对象保存到文件中,而无需使用服务器端语言,那么恐怕这是不可能的。