当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 看了就知道什么是JSON

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 看了就知道什么是JSON


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 145 ::
收藏到网摘: n/a

什么是JSON
JSON(Javascript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于Javascript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, Javascript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。
JSON概念很简单,就是服务器直接生成Javascript语句,客户端获取后直接用eval方法来获得这个对象,这样就可以省去解析XML的性损失。
使用XML表示:
复制代码 代码如下:

<items>
<item>
<id>1</id>
<author>Jackson</author>
<url>http://www.ruanchen.com</url>
<content>Welcome to Web.cn</content>
</item>
<item>
<id>2</id>
<author>Relkn</author>
<url>http://www.ruanchen.com</url>
<content>Web.cn关注互联网新技术</content>
</item>
<item>
<id>3</id>
<author>Kvogend</author>
<url>http://www.ruanchen.com</url>
<content>ruanchen.com软晨学习网</content>
</item>
</items>

代码:
复制代码 代码如下:

<items>
<item>
<id>1</id>
<author>Jackson</author>
<url>http://www.ruanchen.com</url>
<content>Welcome to Web.cn</content>
</item>
<item>
<id>2</id>
<author>Relkn</author>
<url>http://www.ruanchen.com</url>
<content>Web.cn关注互联网新技术</content>
</item>
<item>
<id>3</id>
<author>Kvogend</author>
<url>http://www.ruanchen.com</url>
<content>ruanchen.com软晨学习网</content>
</item>
</items>

使用JSON:
复制代码 代码如下:

{items:[
{
id:1,
author:\"Jackson\",
url:\"http://www.ruanchen.com\",
content:\"Welcome to Web.cn\"
},
{
id:2,
author:\"Relkn\",
url:\"http://www.ruanchen.com\",
content:\"Web.cn关注互联网新技术\"
},
{
id:3,
author:\"Kvogend\",
url:\"http://www.ruanchen.com\",
content:\"ruanchen.com软晨学习网\"
}
]};

代码:
复制代码 代码如下:

{items:[
{
id:1,
author:\"Jackson\",
url:\"http://www.ruanchen.com\",
content:\"Welcome to Web.cn\"
},
{
id:2,
author:\"Relkn\",
url:\"http://www.ruanchen.com\",
content:\"Web.cn关注互联网新技术\"
},
{
id:3,
author:\"Kvogend\",
url:\"http://www.ruanchen.com\",
content:\"ruanchen.com软晨学习网\"
}
]};

JSON不仅减少了解析XML解析带来的性能问题和兼容性问题,而且对于Javascript来说非常容易使用,可以方便的通过遍历数组以及访问对象属性来获取数据,其可读性也不错,基本具备了结构化数据的性质。不得不说是一个很好的办法,而且事实上google maps就没有采用XML传递数据,而是采用了JSON方案。
JSON的另外一个优势是"跨域性",例如你在www.ruanchen.com的网页里使用
<script type="text/javascript" src="" target="_blank">http://www.yyy.com/some.js">
是完全可行的,这就意味着你可以跨域传递信息。而使用XMLHttpRequest却获取不了跨域的信息,这是Javascript内部的安全性质所限制的。
JSON能完全取代XML吗?当然不能,原因就在于XML的优势:通用性。要使服务器端产生语法合格的Javascript代码并不是很容易做到的,这主要发生在比较庞大的系统,服务器端和客户端有不同的开发人员。它们必须协商对象的格式,这很容易造成错误。