﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Charming Java-文章分类-JavaScript</title><link>http://www.blogjava.net/vivianLiu/category/39688.html</link><description>Everyone will enjoy it!</description><language>zh-cn</language><lastBuildDate>Thu, 28 May 2009 08:07:44 GMT</lastBuildDate><pubDate>Thu, 28 May 2009 08:07:44 GMT</pubDate><ttl>60</ttl><item><title>转载：深入浅出JSON </title><link>http://www.blogjava.net/vivianLiu/articles/278201.html</link><dc:creator>vivian</dc:creator><author>vivian</author><pubDate>Wed, 27 May 2009 06:41:00 GMT</pubDate><guid>http://www.blogjava.net/vivianLiu/articles/278201.html</guid><wfw:comment>http://www.blogjava.net/vivianLiu/comments/278201.html</wfw:comment><comments>http://www.blogjava.net/vivianLiu/articles/278201.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vivianLiu/comments/commentRss/278201.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vivianLiu/services/trackbacks/278201.html</trackback:ping><description><![CDATA[<div class="postTitle"><a class="postTitle2" id="AjaxHolder_ctl01_TitleUrl" href="http://www.cnblogs.com/Truly/archive/2006/12/31/608896.html">深入浅出JSON</a> </div>
<p>Author：<a href="http://truly.cnblogs.com/" target="_blank">Truly</a><br />
<br />
<strong>JSON定义<br />
<br />
</strong>&nbsp;&nbsp;&nbsp; JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式，易于阅读和编写，同时也易于机器解析和生成。它基于ECMA262语言规范（1999-12第三版）中JavaScript 编程语言的一个子集。 JSON采用与编程语言无关的文本格式，但是也使用了类C语言（包括C， C++， C#， Java， JavaScript， Perl， Python等）的习惯，这些特性使JSON成为理想的数据交换格式。</p>
<p>JSON的结构基于下面两点<br />
</p>
<ul>
    <li><strong>1. "名称/值"对的集合</strong> 不同语言中，它被理解为对象(object)，记录(record)，结构(struct)，字典(dictionary)，哈希表(hash table)，键列表(keyed list)等</li>
    <li><strong>2. 值的有序列表</strong> 多数语言中被理解为数组(array) </li>
</ul>
<strong>JSON使用</strong>：<br />
<br />
JSON以一种特定的字符串形式来表示 JavaScript 对象。如果将具有这样一种形式的字符串赋给任意一个 JavaScript 变量，那么该变量会变成一个对象引用，而这个对象就是字符串所构建出来的，好像有点拗口，我们还是用实例来说明。
<p>&nbsp;这里假设我们需要创建一个User对象，并具有以下属性</p>
<li>用户ID</li>
<li>用户名</li>
<li>用户Email
<p>您可以使用以下JSON形式来表示User对象：</p>
<pre class="codePanel">{"UserID":11, "Name":"Truly", "Email":"zhuleipro◎hotmail.com"};</pre>
<p>然后如果把这一字符串赋予一个JavaScript变量，那么就可以直接使用对象的任一属性了。</p>
<p>完整代码:</p>
<pre class="codePanel">&lt;script&gt;<br />
var User = {"UserID":11, "Name":"Truly", "Email":"zhuleipro◎hotmail.com"};
alert(User.Name);
&lt;/script&gt;</pre>
<p>实际使用时可能更复杂一点，比如我们为Name定义更详细的结构，使它具有FirstName和LastName：</p>
<pre class="codePanel">{"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"}</pre>
<p>完整代码:</p>
<pre class="codePanel">&lt;script&gt;<br />
var User = {"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"};
alert(User.Name.FirstName);
&lt;/script&gt;</pre>
<p>现在我们增加一个新的需求，我们某个页面需要一个用户列表，而不仅仅是一个单一的用户信息，那么这里就需要创建一个用户列表数组。<br />
下面代码演示了使用JSON形式定义这个用户列表： </p>
<pre class="codePanel">[
{"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"},
{"UserID":12, "Name":{"FirstName":"Jeffrey","LastName":"Richter"}, "Email":"xxx◎xxx.com"},
{"UserID":13, "Name":{"FirstName":"Scott","LastName":"Gu"}, "Email":"xxx2◎xxx2.com"}
]</pre>
<p><br />
完整代码: </p>
<pre class="codePanel">&lt;script&gt;
var UserList = [
{"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"},
{"UserID":12, "Name":{"FirstName":"Jeffrey","LastName":"Richter"}, "Email":"xxx◎xxx.com"},
{"UserID":13, "Name":{"FirstName":"Scott","LastName":"Gu"}, "Email":"xxx2◎xxx2.com"}
];
alert(UserList[0].Name.FirstName);
&lt;/script&gt;</pre>
<p>事实上除了使用"."引用属性外，我们还可以使用下面语句：</p>
<pre class="codePanel">alert(UserList[0]["Name"]["FirstName"]); 或者 alert(UserList[0].Name["FirstName"]); </pre>
<br />
<p>现在读者应该对JSON的使用有点认识了，归纳为以下几点：</p>
</li>
<li>对象是属性、值对的集合。一个对象的开始于&#8220;{&#8221;，结束于&#8220;}&#8221;。每一个属性名和值间用&#8220;:&#8221;提示，属性间用&#8220;,&#8221;分隔。</li>
<li>数组是有顺序的值的集合。一个数组开始于"["，结束于"]"，值之间用","分隔。</li>
<li>值可以是引号里的字符串、数字、true、false、null，也可以是对象或数组。这些结构都能嵌套。</li>
<li>字符串和数字的定义和C或Java基本一致。
<p><strong>小节</strong>：<br />
<br />
本文通过一个实例演示，初步了解了JSON 的强大用途。可以归结如下： </p>
</li>
<li>JSON 提供了一种优秀的面向对象的方法，以便将元数据缓存到客户机上。</li>
<li>JSON 帮助分离了验证数据和逻辑。</li>
<li>JSON 帮助为 Web 应用程序提供了 Ajax 的本质。
<p>参考资料：<br />
<a href="http://www.json.org/">http://www.json.org/</a></p>
</li>
<img src ="http://www.blogjava.net/vivianLiu/aggbug/278201.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vivianLiu/" target="_blank">vivian</a> 2009-05-27 14:41 <a href="http://www.blogjava.net/vivianLiu/articles/278201.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>