如果您想详细了解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>Nach dem Login kopieren
<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>Nach dem Login kopieren
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>Nach dem Login kopieren
<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>Nach dem Login kopieren
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>Nach dem Login kopieren
为什么要加括号?
加上圆括号的目的是迫使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>Nach dem Login kopieren
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>Nach dem Login kopieren
<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>Nach dem Login kopieren
结论
理解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>