Ajax:拥抱JSON,让XML走开

来源:aspxuexi.com 时间:2007-10-12 20:01:00 

什么是JSON http://www.json.org/json-zh.html

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>asp</author>
<url>http://www.aspxhome.com</url>
<content>Welcome to aspxhome.com</content>
</item>
<item>
<id>2</id>
<author>Relkn</author>
<url>http://www.aspxhome.com</url>
<content>aspxhome.com 关注asp</content>
</item>
<item>
<id>3</id>
<author>kak</author>
<url>http://www.aspxhome.com</url>
<content>www.aspxhome.com asp教程</content>
</item>
</items>
<items>
<item>
<id>1</id>
<author>aspxuexi</author>
<url>http://www.aspxhome.com</url>
<content>Welcome to aspxhome.com</content>
</item>
<item>
<id>2</id>
<author>Relkn</author>
<url>http://www.aspxhome.com</url>
<content>aspxhome.com关注互联网新技术</content>
</item>
<item>
<id>3</id>
<author>Kvogend</author>
<url>http://www.aspxhome.com</url>
<content>aspxhome.com关注WEB2.0</content>
</item>
</items>

使用JSON:

{items:[
{
id:1,
author:\"aspxuexi\",
url:\"http://www.aspxhome.com\",
content:\"Welcome to aspxhome.com\"
},
{
id:2,
author:\"Relkn\",
url:\"http://www.aspxhome.com\",
content:\"aspxhome.com关注互联网新技术\"
},
{
id:3,
author:\"Kvogend\",
url:\"http://www.aspxhome.com\",
content:\"aspxhome.com关注WEB2.0\"
}
]};
{items:[
{
id:1,
author:\"aspxuexi\",
url:\"http://www.aspxhome.com\",
content:\"Welcome to aspxhome.com\"
},
{
id:2,
author:\"Relkn\",
url:\"http://www.aspxhome.com\",
content:\"aspxhome.com关注互联网新技术\"
},
{
id:3,
author:\"Kvogend\",
url:\"http://www.aspxhome.com\",
content:\"aspxhome.com关注WEB2.0\"
}
]};

JSON不仅减少了解析XML解析带来的性能问题和兼容性问题,而且对于Javascript来说非常容易使用,可以方便的通过遍历数组以及访问对象属性来获取数据,其可读性也不错,基本具备了结构化数据的性质。不得不说是一个很好的办法,而且事实上google maps就没有采用XML传递数据,而是采用了JSON方案。
JSON的另外一个优势是"跨域性",例如你在www.aspxhome.com的网页里使用

<script type="text/javascript" src="http://www.yyy.com/some.js">

是完全可行的,这就意味着你可以跨域传递信息。而使用XMLHttpRequest却获取不了跨域的信息,这是Javascript内部的安全性质所限制的。
JSON能完全取代XML吗?当然不能,原因就在于XML的优势:通用性。要使服务器端产生语法合格的Javascript代码并不是很容易做到的,这主要发生在比较庞大的系统,服务器端和客户端有不同的开发人员。它们必须协商对象的格式,这很容易造成错误。 

标签:ajax,json,Javascript,xml
0
投稿

猜你喜欢

  • Python unittest单元测试框架及断言方法

    2023-10-29 12:07:48
  • Python 加密与解密小结

    2021-04-28 00:35:47
  • python正则表达式函数match()和search()的区别

    2021-10-05 10:25:52
  • python使用pymongo操作mongo的完整步骤

    2023-07-12 20:31:21
  • CentOS7.2安装MySql5.7并开启远程连接授权的教程

    2024-01-16 15:40:58
  • Python字典和列表性能之间的比较

    2022-08-08 12:49:58
  • Python使用Kubernetes API访问集群

    2023-09-23 05:35:31
  • 详解python中的Turtle函数库

    2021-10-17 19:50:45
  • Python NumPy实现数组排序与过滤示例分析讲解

    2021-07-20 08:33:20
  • 创建pycharm的自定义python模板方法

    2021-01-11 04:46:41
  • python自动化测试工具Helium使用示例

    2022-09-26 22:59:05
  • 使用python如何删除同一文件夹下相似的图片

    2021-10-19 02:52:23
  • python字典如何获取最大和最小value对应的key

    2021-07-10 11:14:22
  • SQL Server误区30日谈 第6天 有关NULL位图的三个误区

    2024-01-21 22:21:26
  • golang常用库之pkg/errors包第三方错误处理包案例详解

    2024-02-14 09:20:38
  • Sun正式发布MySQL 5.1版 简化数据库应用

    2008-12-11 15:15:00
  • 使用Python爬取最好大学网大学排名

    2023-09-17 09:24:45
  • 了解WEB页面工具语言XML(二)定义

    2008-09-05 17:18:00
  • Django values()和value_list()的使用

    2021-08-21 23:03:12
  • Python 高级变量之字典和字符串详解

    2021-11-16 19:36:35
  • asp之家 网络编程 m.aspxhome.com