如果您想详细了解ev
ev
JSON:http://www.json.org/
ev
ev
举例说明
123 <span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> bar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'bar'</span></span><span class="sh_symbol" style="color: #8b0000;">;</span><span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> foobar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'"foo" + bar'</span></span><span class="sh_symbol" style="color: #8b0000;">);</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span>foobar<span class="sh_symbol" style="color: #8b0000;">);</span>
12345678910 <span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> bar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'bar'</span></span><span class="sh_symbol" style="color: #8b0000;">;</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// if variable bar equals 'bar', foobar is the result of</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// last executing statement: bar="foo-bar";</span><span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> foobar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}'</span></span><span class="sh_symbol" style="color: #8b0000;">);</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span>foobar<span class="sh_symbol" style="color: #8b0000;">);</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// change the value</span>bar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'foo'</span></span><span class="sh_symbol" style="color: #8b0000;">;</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// now our the last executed statement is: bar = "bar-foo";</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// therefore the value of variable foobar has been changed</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// into 'bar-foo'</span>foobar <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}'</span></span><span class="sh_symbol" style="color: #8b0000;">);</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span>foobar<span class="sh_symbol" style="color: #8b0000;">);</span>
JSON的格式
JSON的格式是由大括号和由冒号(:)构成的名值对所组成的。注意JSON格式与对象字面量 (object literals) 的区别:JSON的名字部分严格用引号+名字来表示。
举例说明
12345678 <span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> objectLiteral <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_cbracket" style="color: red;">{</span><span class="sh_predef_var" style="color: #00008b;">name</span><span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"Objector.L"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span>age<span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"24"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span>special<span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"JavaScript"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span>sayName<span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_keyword" style="font-weight: bold; color: blue;">function</span><span class="sh_symbol" style="color: #8b0000;">()</span> <span class="sh_cbracket" style="color: red;">{</span><span class="sh_keyword" style="font-weight: bold; color: blue;">return</span> <span class="sh_keyword" style="font-weight: bold; color: blue;">this</span><span class="sh_symbol" style="color: #8b0000;">.</span><span class="sh_predef_var" style="color: #00008b;">name</span><span class="sh_symbol" style="color: #8b0000;">;</span><span class="sh_cbracket" style="color: red;">}</span><span class="sh_cbracket" style="color: red;">}</span><span class="sh_symbol" style="color: #8b0000;">;</span>
12345678910111213 <span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> jsonFormat <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"summary"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"Blogs"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"blogrolls"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_symbol" style="color: #8b0000;">[</span><span class="sh_cbracket" style="color: red;">{</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"title"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"Explore JavaScript"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;"> "link"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span style="font-family: 新宋体;"><span class="sh_string" style="color: red;">"http://example.com/"</span></span> <span class="sh_cbracket" style="color: red;">}</span><span class="sh_symbol" style="color: #8b0000;">,</span> <span class="sh_cbracket" style="color: red;">{</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;"> "title"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"Explore JavaScript"</span></span><span class="sh_symbol" style="color: #8b0000;">,</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;"> "link"</span></span><span class="sh_symbol" style="color: #8b0000;">:</span> <span style="font-family: 新宋体;"><span class="sh_string" style="color: red;">"http://example.com/"</span></span> <span class="sh_cbracket" style="color: red;">}</span><span class="sh_symbol" style="color: #8b0000;">]</span><span class="sh_cbracket" style="color: red;">}</span><span class="sh_symbol" style="color: #8b0000;">;</span>
ev
由于Ajax的兴起,JSON这种轻量级的数据格式作为客户端与服务器之间的传输格式逐渐地流行起来,进而出现的问题是如何将服务器端构建好的JSON数据转化为可用的JavaS
1 <span class="sh_keyword" style="font-weight: bold; color: blue;">var</span> jsonObject <span class="sh_symbol" style="color: #8b0000;">=</span> <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"("</span></span> <span class="sh_symbol" style="color: #8b0000;">+</span> jsonFormat <span class="sh_symbol" style="color: #8b0000;">+</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">")"</span></span><span class="sh_symbol" style="color: #8b0000;">);</span>
为什么要加括号?
加上圆括号的目的是迫使ev
12 <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"{}"</span></span><span class="sh_symbol" style="color: #8b0000;">);</span> <span class="sh_comment" style="font-style: italic; color: #a52a2a;">// return undefined</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">"({})"</span></span><span class="sh_symbol" style="color: #8b0000;">);</span><span class="sh_comment" style="font-style: italic; color: #a52a2a;">// return object[Object]</span>
JSON格式的名字部分为什么要加引号?
因为ev
举例说明
1 <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'{foo:"bar"}'</span></span><span class="sh_symbol" style="color: #8b0000;">));</span> <span class="sh_comment" style="font-style: italic; color: #a52a2a;">// return "bar", incorrect</span>
1 <span class="sh_predef_func" style="font-weight: bold; color: #00008b;">alert</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'({"foo": "bar"})'</span></span><span class="sh_symbol" style="color: #8b0000;">));</span> <span class="sh_comment" style="font-style: italic; color: #a52a2a;">// return JSON object, correct</span>
结论
理解ev
following this format:
1 |
<span class="sh_predef_func" style="font-weight: bold; color: #00008b;">eval</span><span class="sh_symbol" style="color: #8b0000;">(</span><span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">'{'</span></span> <span class="sh_symbol" style="color: #8b0000;">+</span> jsonString <span class="sh_symbol" style="color: #8b0000;">+</span> <span class="sh_string" style="color: red;"><span style="font-family: 新宋体;">')'</span></span><span class="sh_symbol" style="color: #8b0000;">);</span> |
from:http://www.jb51.net/article/21688.htm