从json字符串传递复杂的javascript对象配置(数据和函数)
我有一个javascript,里面有一堆参数和函数从json字符串传递复杂的javascript对象配置(数据和函数),javascript,json,metaprogramming,Javascript,Json,Metaprogramming,我有一个javascript,里面有一堆参数和函数 ctrl.kendoGrid({ dataSource: { type: "odata", transport: { read: { url: "odata/CodeView", dataType: "json", contentType: "application/json" }, update: {
ctrl.kendoGrid({
dataSource: {
type: "odata",
transport: {
read: {
url: "odata/CodeView",
dataType: "json",
contentType: "application/json"
},
update: {
url: function (data) {
return "api/CodeMapUpdate/" + data.CODE_MAP_ID;
},
dataType: "json",
type: "post",
complete: function (e) {
ctrl.data("kendoGrid").dataSource.read();
if (e.status == 201) {
logger.log("Record Updated: Record ID = " + e.responseJSON, null, null, true);
} else {
logger.logError(" Save failed " + e.responseJSON.ExceptionMessage, null, null, true);
}
}
},
destroy: {
url: function (data) {
return "api/CodeMapDelete/" + data.CODE_MAP_ID;
},
dataType: "json",
complete: function () {
ctrl.data("kendoGrid").dataSource.read();
}
},
create: {
url: "api/CodeMapCreate",
dataType: "json",
complete: function (e) {
ctrl.data("kendoGrid").dataSource.sort({
field: "CODE_MAP_ID",
dir: "desc"
});
ctrl.data("kendoGrid").dataSource.filter({});
if (e.status == 201) {
logger.log("Record Created: Record ID = " + e.responseJSON, null, null, true);
} else {
logger.logError(" Save failed " + e.responseJSON.ExceptionMessage, null, null, true);
}
}
},
},
schema: {
data: function (data) {
return data.value;
},
total: function (data) {
return data["odata.count"];
},
model: {
id: "CODE_MAP_ID",
fields: {
CODE_MAP_ID: {
editable: false,
type: "number"
},
CODE_NAME: {
type: "string",
validation: {
title: "Required Field",
required: true
}
},
L_NAME: {
type: "string",
validation: {
required: true
}
},
CODE_DATE: {
field: "CODE_DATE",
type: "date",
format: "{0:MM/dd/yyyy}",
validation: {
required: true
}
},
}
}
},
change: function () {
},
//batch: true,
pageSize: 20,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
sort: {
field: "CODE_MAP_ID",
dir: "desc"
}
//autoSync: true
} })
我试图将整个“数据源”对象保存为变量,并在运行时使用ajax检索它。我可以用eval(“(“+dataSource+”)实现这一点,但任何包含的函数都不再执行
有没有关于在JSON中存储/检索此类对象的策略的想法?下面是一个简单的演示,演示如何使用JSON.parse的第二个参数来恢复存储的函数:
var ob={a:1, b:false, c: function(a){ return a * a;} };//a sample object
Function.prototype.toJSON=Function.toString; //extend JSON to cover Functions
var str=JSON.stringify(ob, null, "\t"); //turn sample object into a string:
/* str =={
"a": 1,
"b": false,
"c": "function (a){return a*a;}"
} */
//now turn the string back into an object, using a reviver to re-parse methods:
var ob2=JSON.parse(str, function(a,b){
if(b.match && b.match(/^function[\w\W]+\}$/)){ b=eval("b=0||"+b); }
return b;
});
var n=5; //let's try the method using a number
var n2=ob2.c(5); //apply the method to the number
alert(n2); // shows: 25, the number times itself, verifying that the function works
您可能希望对发送给eval的内容更加严格,除了匹配看起来像函数的属性外,还可以使用密钥模式。您可以将regexp扩展得更严格一些,但是对于这个JSON.parse()参数的快速演示,一切都很好
在本例中,由于您正在收集JSON的属性,因此不可能遇到eval()所使用的安全问题。这些问题源于将一个用户的输入发送给另一个用户而不进行过滤,而不是在您上次跳转启动客户机本身生成的代码时…Function.prototype.toJSON=Function.toString,使用regexp验证parse()时恢复。请详细说明一下,也许是一些示例代码