开发者

Why eval fails here?

开发者 https://www.devze.com 2023-01-27 16:12 出处:网络
0001: response $[0] = [string] \"{\\\"code\\\":200,\\\"id\\\":121}\" 0001: eval(response) Synta开发者_开发知识库xError: invalid label
0001: response
$[0] = [string] "{\"code\":200,\"id\":121}"
0001: eval(response)
Synta开发者_开发知识库xError: invalid label

Anyone knows?


You have to wrap it in () to trigger expression evaluation, like this:

eval("(" + response + ")")

You can test it out here.


Though a better method is native JSON handling:

var result = JSON.parse(response);

Just include json2.js for older browser (< IE8) support, the call is the same...it just adds the global JSON object if it's missing.


You need to wrap the JSON string in parentheses.

Otherwise, the { ... } is interpreted as a block of executable statements, which it isn't.

By surrounding it in parentheses, you force the interpreter to interpret it as an expression.

0

精彩评论

暂无评论...
验证码 换一张
取 消