gpt4 book ai didi

javascript - 将带有 JavaScript 函数的 javascript 数组转换为 json 并返回

转载 作者:行者123 更新时间:2023-11-27 23:53:39 24 4
gpt4 key购买 nike

我有一个包含函数的 JavaScript 数组/对象,例如

var map = [function(){alert('test') }, function(){alert('test2') }];

如何将此数组转换为 JSON 并转换回可用的 JavaScript 代码?

我应该使用 eval 还是预先创建这些函数,然后使用 window["functionname"] 加载它们

最佳答案

这是一种可以做到这一点的方法。

要转换为 JSON,您可以这样做:

//your original map
var map = [function(){ alert('test') }, function(){ alert('test2') }];
//convert it to a string
var str = map.toString();
//add the string to a javascript object
var obj = {funcArrStr: str};
//convert that object to json
var json = JSON.stringify(obj);

然后为了解析,你可以这样做:

var json = /* get json from server side */;
//parse the json
var parsed = JSON.parse(json);
//pull out the string from the parsed object
var funcArrStr = parsed.funcArrStr;
//wrap it in brackets [] and eval it to convert back to array
var evaled = eval('[' + parsed.funcArrStr + ']');

运行下面的代码片段并检查浏览器的 JavaScript 控制台以获取演示。

var map = [function(){ alert('test') }, function(){ alert('test2') }];
console.log('map:');
console.log(map);

var str = map.toString();
console.log('convert map to string:');
console.log(str);

var obj = {funcArrStr: str};
console.log('put it in an object:');
console.log(obj);

var json = JSON.stringify(obj);
console.log('convert to json string:');
console.log(json);

var parsed = JSON.parse(json);
console.log('parse json string:');
console.log(parsed);

var funcArrStr = parsed.funcArrStr;
console.log('pull out string from object');
console.log(funcArrStr);

var evaled = eval('[' + parsed.funcArrStr + ']');
console.log('wrap in [] and eval:');
console.log(evaled);

编辑回应评论

How would eval affect my javascript speed if I have lets say an array containing a few hundred functions?

测试一下。在我的浏览器中,分配字符串、调用 eval 创建函数数组以及对下面代码片段中的 300 多个函数调用 eval 通常需要大约 2 毫秒。如果您有较大的函数,很可能会花费更长的时间,但请对其进行测试并计时以查看加载时间是否合理。

var start = new Date().getMilliseconds();

var funcStr = "function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }," +
"function() { alert(test); }";

var functions = eval('[' + funcStr + ']');

for (var i = 0; i < functions.length; i++) {
eval(functions[i]);
}
var end = new Date().getMilliseconds();
alert('Evals complete in ' + (end-start) + ' milliseconds');

关于javascript - 将带有 JavaScript 函数的 javascript 数组转换为 json 并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32466548/

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