Maison >interface Web >js tutoriel >javascript eval和JSON之间的联系_javascript技巧
如果您想详细了解ev
ev
JSON:http://www.json.org/
ev
ev
举例说明
<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>
<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>
<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>
ev
由于Ajax的兴起,JSON这种轻量级的数据格式作为客户端与服务器之间的传输格式逐渐地流行起来,进而出现的问题是如何将服务器端构建好的JSON数据转化为可用的JavaS
<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>
为什么要加括号?
加上圆括号的目的是迫使ev
<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格式的名字部分为什么要加引号?
因为ev
举例说明
<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>
<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>
结论
理解ev
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>