首页 >web前端 >js教程 >javascript eval和JSON之间的联系_javascript技巧

javascript eval和JSON之间的联系_javascript技巧

WBOY
WBOY原创
2016-05-16 18:37:23879浏览

如果您想详细了解eval和JSON请参考以下链接:

eval  :https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval

JSON:http://www.json.org/

eval函数的工作原理

eval函数会评估一个给定的含有JavaScript代码的字符串,并且试图去执行包含在字符串里的表达式或者一系列的合法的JavaScript语句。eval函数将把最后一个表达式或者语句所包含的值或引用作为返回值。

举例说明

  • eval评估JavaScript表达式
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">var</SPAN> bar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'bar'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px"><BR>var</SPAN> foobar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'"foo" + bar'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px"><BR>alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN>foobar<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN>
  • eval评估JavaScript语句
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">var</SPAN> bar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'bar'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic"><BR>// if variable bar equals 'bar', foobar is the result of <BR></SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// last executing statement: bar="foo-bar";</SPAN><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px"><BR>var</SPAN> foobar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px"><BR>alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN>foobar<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// change the value</SPAN>bar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'foo'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic"><BR>// now our the last executed statement is: bar = "bar-foo";</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic"><BR>// therefore the value of variable foobar has been changed</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic"><BR>// into 'bar-foo'</SPAN><BR>foobar <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px"><BR>alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN>foobar<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN>

JSON的格式

JSON的格式是由大括号和由冒号(:)构成的名值对所组成的。注意JSON格式与对象字面量 (object literals) 的区别:JSON的名字部分严格用引号+名字来表示。

举例说明

  • 对象的字面量
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">var</SPAN> objectLiteral <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">{</SPAN><BR>  <SPAN class=sh_predef_var style="COLOR: #00008b; LINE-HEIGHT: 20px">name</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"Objector.L"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,</SPAN><BR>  age<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"24"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,</SPAN><BR>  special<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"JavaScript"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,</SPAN><BR>  sayName<SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">function</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">()</SPAN> <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">{</SPAN><BR>    <SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">return</SPAN> <SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">this</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">.</SPAN><SPAN class=sh_predef_var style="COLOR: #00008b; LINE-HEIGHT: 20px">name</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN><BR>  <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">}</SPAN><SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px"><BR>}</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN>
  • JSON对象
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">var</SPAN> jsonFormat <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">{<BR></SPAN>  <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"summary"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"Blogs"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,<BR></SPAN>  <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"blogrolls"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">[</SPAN><BR>    <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">{<BR></SPAN>       <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"title"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"Explore JavaScript"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,</SPAN><BR>      <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体> "link"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <FONT face=新宋体><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace">"http://example.com/"</SPAN><BR></FONT>    <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">}</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,<BR></SPAN>    <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">{</SPAN> <BR>      <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体> "title"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"Explore JavaScript"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">,</SPAN><BR>      <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体> "link"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">:</SPAN> <FONT face=新宋体><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace">"http://example.com/"</SPAN><BR></FONT>    <SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px">}</SPAN><BR>  <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">]</SPAN><SPAN class=sh_cbracket style="COLOR: red; LINE-HEIGHT: 20px"><BR>}</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">;</SPAN>

eval和JSON

由于Ajax的兴起,JSON这种轻量级的数据格式作为客户端与服务器之间的传输格式逐渐地流行起来,进而出现的问题是如何将服务器端构建好的JSON数据转化为可用的JavaScript对象。利用eval函数无疑是一种简单而直接的方法。在转化的时候需要将JSON字符串的外面包装一层圆括号:

<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_keyword style="FONT-WEIGHT: bold; COLOR: blue; LINE-HEIGHT: 20px">var</SPAN> jsonObject <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">=</SPAN> <SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"("</FONT></SPAN> <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">+</SPAN> jsonFormat <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">+</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>")"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN>

为什么要加括号?

加上圆括号的目的是迫使eval函数在评估JavaScript代码的时候强制将括号内的表达式(expression)转化为对象,而不是作为语句(statement)来执行。举一个例子,例如对象字面量{},如若不加外层的括号,那么eval会将大括号识别为JavaScript代码块的开始和结束标记,那么{}将会被认为是执行了一句空语句。所以下面两个执行结果是不同的:

<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"{}"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN> <SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// return undefined</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px"><BR>alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>"({})"</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN><SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// return object[Object]</SPAN>

JSON格式的名字部分为什么要加引号?

因为eval函数会将{foo:”bar”}解释成合法的JavaScript语句,而非表达式。但是人们往往想要的是让eval将这段代码解释成一个对象。所以JSON格式会强制你去在名字的外侧加上引号再结合圆括号,这样eval就不会错误的将JSON解释成代码块。

举例说明

  • eval错误解析语义
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'{foo:"bar"}'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">));</SPAN>   <SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// return "bar", incorrect</SPAN>
  • eval正确解析JSON
<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">alert</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'({"foo": "bar"})'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">));</SPAN> <SPAN class=sh_comment style="COLOR: #a52a2a; LINE-HEIGHT: 20px; FONT-STYLE: italic">// return JSON object, correct</SPAN>

结论

理解eval的工作原理和json的严格的限定格式,合理结合eval和json应用于JavaScript的数据传递和对象转换。

following this format:

<span style="FONT-SIZE: medium; LINE-HEIGHT: normal; FONT-FAMILY: Calibri; WHITE-SPACE: normal"><PRE class="sh_sourceCode sh_javascript_dom"><SPAN class=sh_predef_func style="FONT-WEIGHT: bold; COLOR: #00008b; LINE-HEIGHT: 20px">eval</SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">(</SPAN><SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>'{'</FONT></SPAN> <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">+</SPAN> jsonString <SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">+</SPAN> <SPAN class=sh_string style="COLOR: red; LINE-HEIGHT: 20px; FONT-FAMILY: monospace"><FONT face=新宋体>')'</FONT></SPAN><SPAN class=sh_symbol style="COLOR: #8b0000; LINE-HEIGHT: 20px">);</SPAN>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn