最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How do I convert a JSON string to a function in javascript? - Stack Overflow

programmeradmin0浏览0评论

How can I convert a string in javascript/jquery to a function?

I am trying to use a JSON parameter list to initialize a function. However, one of the parameters is a function, which I store as a string, and I get an error when I try to use eval() to return the function.

For example, if my JSON is:

json = { "one": 700, "two": "function(e){alert(e);}" }

Then in my code:

parameters = eval(json);
$('myDiv').addThisFeature({
 parameter_1: json.one,
 parameter_2: eval(json.two)  // <= generates error
})

How can I convert a string in javascript/jquery to a function?

I am trying to use a JSON parameter list to initialize a function. However, one of the parameters is a function, which I store as a string, and I get an error when I try to use eval() to return the function.

For example, if my JSON is:

json = { "one": 700, "two": "function(e){alert(e);}" }

Then in my code:

parameters = eval(json);
$('myDiv').addThisFeature({
 parameter_1: json.one,
 parameter_2: eval(json.two)  // <= generates error
})
Share Improve this question asked Dec 14, 2010 at 18:26 Adam MorrisAdam Morris 8,54513 gold badges47 silver badges68 bronze badges 1
  • Thanks - I do know JSON is data-only... I'm trying to initialize jqGrid object with a changing parameter list, and I occasionally use one parameter takes a function... and in this case, as the function itself has changing internal data, it's easier to generate it and send it across as a string, then define it in my library and initialize it separately. – Adam Morris Commented Dec 14, 2010 at 18:53
Add a comment  | 

6 Answers 6

Reset to default 7

Example: http://jsfiddle.net/patrick_dw/vs83H/

var json = '{ "one": 700, "two": "function(e){alert(e);}" }';
var parameters = JSON.parse( json );
eval( 'var func = ' + parameters.two );
func( 'test' ); // alerts "test"

You'll need to load the JSON library in browsers that don't support it.

Or do two separate evals:

Example: http://jsfiddle.net/patrick_dw/vs83H/1/

var json = '{ "one": 700, "two": "function(e){alert(e);}" }';
eval( 'var parameters = ' + json );
eval( 'var func = ' + parameters.two );
func( 'test' );

I assume you're aware of the dangers of eval.

Looking for a way to not use eval this is the best I could come up with. Use the Function constructor to create a function from a string.

var parsed = JSON.parse('{"one":"700", "two":"function(){return 0;}" }');
var func = new Function('return ' + parsed.two)(); // return parsed.two function
alert(typeof func); // function
alert(func()) // 0

Use this:

parameters = eval('(' + json + ')');
$('#myDiv').addThisFeature({
 parameter_1: parameters.one,
 parameter_2: eval('(' + parameters.two + ')')  // <= does not generate an error
});

Adding the parentheses at the beginning and end of the string prevents the syntax error.

Note, however, that you are parsing JSON using eval (which in some cases has security risks, but I assume that is irrelevant because you do want to run arbitrary code sent by the server). If you have the server-side flexibility (to send invalid JSON), you could just send the function not quoted as a string and eval should be able to parse that just fine.

See this SO question. As was said, JSON is meant to hold data. To treat a piece of the data as a function, you would first need to eval the string.

You are eval'ing an anonymous function, which of course won't be called by anything. If you really wanted to run the code in the json then the text would need to be alert(e).

However it doesn't sound like a very sensible thing to do. You'd be better off writing code to deal with the contents of the json object, rather than trying to run code embedded in the json.

Neither way is particularly nice, but if you can get rid of the function(e) wrapper bits, then you can use var myDynamicFunction = new Function("e", "alert(e);"); Otherwise, you're looking at using eval(). Eval() is evil in general. If this is JSON that you're getting back from a $.getJSON call or something, you're opening yourself up to security concerns.

发布评论

评论列表(0)

  1. 暂无评论