ajax请求json vs xml的优缺点是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/584985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the advantages and disadvantages of json vs xml for ajax requests?
提问by flybywire
What are the advantages and disadvantages of json vs xml for ajax requests? Is there a difference in performance? i.e. are browsers able to process one format faster than the other?
ajax请求json vs xml的优缺点是什么?性能上有区别吗?即浏览器能够比另一种更快地处理一种格式吗?
采纳答案by John Leidegren
In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects
总之,JSON(可以认为是 JavaScript 的一个子集)比 XML 精简很多。这有几个积极的副作用
- JSON is smaller than corresponding XML
- JSON is faster, i.e. simpler syntax
->easier parsing (faster parsing)
- JSON 比对应的 XML 小
- JSON 更快,即更简单的语法
->更容易解析(faster parsing)
In my original answer to this question, my view of JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.orgdoes a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parseand JSON.stringifywhen not supported by browsers.
在我对这个问题的最初回答中,我对 JSON 的看法是对 JavaScript 的看法,我认为它是近亲。但是 JSON 是独立的,JSON.org在描述 JSON方面做得很好。它也提供了一个JavaScript的兼容性库,增加了支持JSON.parse,并JSON.stringify在浏览器不支持。
While evalat the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.
虽然eval当时(2009 年中)用于评估 JavaScript,但它也可以评估 JSON,即解析 JSON,但它被认为是不安全的,因为它确实允许执行任意 JavaScript 来代替它。
JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.
由于 JSON 与 JavaScript 的密切关系,JSON 恰好非常适合浏览器,并且是发展平台的自然方式。
While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.
虽然 XML 可能被认为具有更好的严谨性,因为您可以键入它,但也正是这些原因使它变慢了(在我看来它也有点冗长)。但如果这是您真正想要的东西,您应该使用它,XML 同样无处不在。
I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.
我不会就动态类型或静态类型进行辩论,但我会这样说。在无模式数据之上添加东西真的很容易,并且有很多方法可以进行验证,无论模式或无模式。
回答by Guillaume
One advantage of XML I havent seen sofar in the discussion is that XML can have schema. This is of great value in describing the structure of the XML. For simple data structure, JSON and a bit of text describing what you are doing is fine. When working with more complex data structures, or when the creator and the consumers of the data are not the same team, having a Schema can help the communication a lot.
到目前为止,我在讨论中还没有看到 XML 的一个优点是 XML 可以有模式。这对于描述 XML 的结构非常有价值。对于简单的数据结构,JSON 和一些描述您正在做什么的文本就可以了。当处理更复杂的数据结构时,或者当数据的创建者和消费者不是同一个团队时,拥有 Schema 可以极大地帮助沟通。
Also, having a schema means that you can validate your data, which can be life saving when trying to debug complexe errors ...
此外,拥有架构意味着您可以验证数据,这在尝试调试复杂错误时可以挽救生命......
回答by VonC
You have in this article "The AJAX response: XML, HTML, or JSON?" a full debate on that topic:
您在这篇文章“ AJAX 响应:XML、HTML 或 JSON?”中有关于该主题的完整辩论:
XML
XML
- Advantages
The most important advantage of XML is that it's the most easily readable format for other humans.
A secondary advantage is that XML has been around for quite a while and that many developers are already accustomed to it. - Disadvantages
The JavaScript required to insert the data into the HTML page is quite verbose.
- 优点
XML 最重要的优点是它是其他人最容易阅读的格式。
第二个优势是 XML 已经存在了很长一段时间,而且许多开发人员已经习惯了它。 - 缺点
将数据插入 HTML 页面所需的 JavaScript 非常冗长。
JSON
JSON
- Advantages
The most important advantage is that JSON circumvents JavaScript's same-source policy, if you import the JSON file as a new<script>tag. See Simon Willison's examplefor the gory details.
JavaScript does not allow you to access documents (be they XML or HTML) that come from another server. However, if you import a JSON file as a script tag you circumvent this problem, and any JSON data can be imported into any website. It depends on your business goals whether this is a Good or a Bad Thing, but right now it's the only data format that allows unrestricted access.
A secondary advantage is that scripts for JSON data are slightly simpler and slightly more in line with the rest of the JavaScript language than scripts for XML data. - Disadvantages
The most important disadvantage of JSON is that the format is very hard to read for humans, and that, of course, every single comma, quote, and bracket should be in exactly the correct place. While this is also true of XML, JSON's welter of complicated-looking syntax, like the }}]} at the end of the data snippet, may frighten the newbies and make for complicated debugging.
- 优点
最重要的优点是,如果您将 JSON 文件作为新<script>标签导入,则 JSON 可以绕过 JavaScript 的同源策略。有关血腥细节,请参阅Simon Willison 的示例。
JavaScript 不允许您访问来自另一台服务器的文档(无论是 XML 还是 HTML)。但是,如果您将 JSON 文件作为脚本标签导入,则可以避免此问题,并且任何 JSON 数据都可以导入任何网站。这是好事还是坏事取决于您的业务目标,但目前它是唯一允许无限制访问的数据格式。
第二个优点是 JSON 数据的脚本比 XML 数据的脚本更简单,并且更符合 JavaScript 语言的其他部分。 - 缺点
JSON 最重要的缺点是格式对于人类来说很难阅读,当然,每个逗号、引号和括号都应该在正确的位置。虽然这也适用于 XML,但 JSON 的一堆看起来很复杂的语法,比如数据片段末尾的 }}]},可能会吓到新手并进行复杂的调试。
From the comments, JSON is considered faster to process than XML.
从评论来看,JSON 被认为比 XML 的处理速度更快。
回答by KarstenF
One advantage of XML that hasn't been mentioned yet is that it is possible to use XSLT to extract data from an XML document using arbitrarily complex XPath expressions. I can't see a way to do this in JSON. How would you find all objects in a JSON document that contain a "currency" field as a direct descendant, irrespective of where the objects occur in the structure? In XML with XSLT/XPath this is very easy.
尚未提及的 XML 的一个优点是可以使用 XSLT 从使用任意复杂 XPath 表达式的 XML 文档中提取数据。我看不到在 JSON 中执行此操作的方法。您将如何在 JSON 文档中查找包含作为直接后代的“货币”字段的所有对象,而不管对象在结构中的位置?在带有 XSLT/XPath 的 XML 中,这很容易。
However, doing this in the browser comes at a price. You will run into browser-specific quirks, and it will probably be slower and more memory intensive.
但是,在浏览器中执行此操作是有代价的。您会遇到特定于浏览器的怪癖,它可能会更慢且占用更多内存。
回答by StaxMan
While I like Json and would recommend it, I think that there is no fundamental difference between optimal processing speed. Differences between libraries and platforms are more significant: a good xml parser is faster than bad json parser and vice versa. So usually performance of formats themselves is not a big factor. Both can be lighting fast (Java has a few good parsers for both, for example, other languages problably too).
虽然我喜欢 Json 并会推荐它,但我认为最佳处理速度之间没有根本区别。库和平台之间的差异更为显着:好的 xml 解析器比糟糕的 json 解析器快,反之亦然。所以通常格式本身的性能不是一个很大的因素。两者都可以快速点亮(Java 有一些很好的解析器,例如,其他语言也可能)。
As to compactness, that depends on type of data, Json is often bit more compact, but not radically so. Except if you have lots of arrays/lists, where json notation "{ ... }" is much more compact than xml tagging (unless you use white space if possible, like "1 2 3 4").
至于紧凑性,这取决于数据类型,Json 通常更紧凑一些,但并非完全如此。除非您有很多数组/列表,其中 json 符号“{ ... }”比 xml 标记更紧凑(除非您尽可能使用空格,例如“1 2 3 4”)。
回答by Richard Levasseur
The link dirkgently provides has a good synopsis of the differences (scroll to the bottom). Important points are:
该链接直接提供了一个很好的差异概要(滚动到底部)。要点是:
JSON: smaller, can represent all unicode characters (xml can't in UTF8, at the least, by its own spec). Its also trivial to make use of it in AJAX applications (because of javascript's eval() function), especially more advanced features like jsonp (responses invoke callbacks).
JSON:更小,可以表示所有 unicode 字符(xml 不能在 UTF8 中,至少,根据它自己的规范)。在 AJAX 应用程序中使用它也很简单(因为 javascript 的 eval() 函数),尤其是更高级的功能,如 jsonp(响应调用回调)。
XML: Has lots of tools to make querying its structure easy. Since its older and more established, it also has more tool support in general.
XML:有很多工具可以轻松查询其结构。由于它更老,更成熟,它也有更多的工具支持。
In general, they can accomplish the same things (you could do jsonp through xml, it would just require manually parsing out the callback).
通常,它们可以完成相同的事情(您可以通过 xml 执行 jsonp,它只需要手动解析回调)。
回答by dirkgently
You may want to read JSON: The Fat-Free Alternative to XML. With JSON you can fool around with callbacks to and fro between the source and the destination of a request and actually use it painlessly in your existing Javascript code.
您可能想阅读JSON: The Fat-Free Alternative to XML。使用 JSON,您可以在请求的源和目标之间来回进行回调,并在您现有的 Javascript 代码中轻松使用它。
回答by Trio Cheung
i think another advantage of Json is that it doesn't use <> brackets and you can put html code in it without a lot of confusion.
我认为 Json 的另一个优点是它不使用 <> 括号,您可以在其中放置 html 代码而不会造成很多混淆。
回答by BitByte_Bake
JSON is lightweight, AJAX friendly(considered as subset of Javascript) and easily serializable. XMLdo all this but after drinking some milk.
JSON 是轻量级的、AJAX 友好的(被视为 Javascript 的子集)并且易于序列化。 XML做了所有这些,但在喝了一些牛奶之后。

