哪个更好:Json 或 XML (PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1116825/
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's better: Json or XML (PHP)
提问by Omar Abid
I'm manipulating few data with PHP, I have the choice between Json and XML. But I don't know what to choose, I never worked with one of them. So I want the easiest one.
我正在用 PHP 处理少量数据,我可以在 Json 和 XML 之间进行选择。但我不知道该选择什么,我从未与其中之一合作过。所以我想要最简单的。
Also I wonder if there's good classes that can make parsing (XML or Json) easier.
另外我想知道是否有好的类可以使解析(XML 或 Json)更容易。
I focus mainly on ease of use rather than speed and scale.
我主要关注易用性而不是速度和规模。
回答by geowa4
json.orgmakes JSON parsing simple. SimpleXMLmakes XML simple for PHP.
json.org使 JSON 解析变得简单。SimpleXML使 PHP 的 XML 变得简单。
When choosing which one, it really depends on your application. If you will be passing data to JavaScript for processing on the client-side, then I recommend JSON. IMO, XML is better for data interchange between systems because it is very well understood and human readable (making interpreting easier for you). JSON is smaller over the wire, as there are no closing tags.
在选择哪一个时,这实际上取决于您的应用程序。如果您要将数据传递给 JavaScript 以在客户端进行处理,那么我推荐 JSON。IMO,XML 更适合系统之间的数据交换,因为它非常易于理解且人类可读(使您更容易解释)。由于没有结束标记,因此 JSON 在网络上更小。
回答by Dykam
JSON parsing and generating is natively included in php: http://www.php.net/json
JSON 解析和生成原生包含在 php 中:http: //www.php.net/json
回答by Prisoner ZERO
I feel the following StackOverflow entries answer your question best:
我觉得以下 StackOverflow 条目最能回答您的问题:
As for which one is easiest?
jQuery makes serializing your JavaScript objects into JSON objects easy.
至于哪个最容易?
jQuery 可以轻松地将 JavaScript 对象序列化为 JSON 对象。
But, if you're still unsure...
但是,如果您仍然不确定...
Here are some other articles:
以下是一些其他文章:
回答by Peter ?tibrany
JSON views your data as structures known from programming languages: maps/dictionaries (called 'objects' in JSON), arrays and literals. JSON puts these basic constructs into hierarchies.
JSON 将您的数据视为编程语言中已知的结构:地图/字典(在 JSON 中称为“对象”)、数组和文字。JSON 将这些基本结构置于层次结构中。
XML is also hierarchical, but it has three basic parts: elements (with start/end tags), attributes within elements, and text content. Elements and text content can be mixed, so it is suited to marking up documents. JSON isn't very good for this.
XML 也是分层的,但它具有三个基本部分:元素(带有开始/结束标记)、元素内的属性和文本内容。元素和文本内容可以混合使用,因此适合标记文档。JSON 不是很好。
XML has huge ecosystem built around it with lot of ready tools: various schemas to check that your XML documents are valid, transformation language, XPath for navigating your document, etc. JSON is lacking in this area too.
XML 围绕它构建了庞大的生态系统,其中包含许多现成的工具:用于检查 XML 文档是否有效的各种模式、转换语言、用于导航文档的 XPath 等。JSON 在这方面也很缺乏。
XML is much more complex than JSON though. It's easy and fun to write your own JSON parser, but writing XML parser is huge undertaking(surely not for somebody who is new to XML). In Javascript, JSON can be parsed by evaluating the text (this is pretty insecure though).
不过,XML 比 JSON 复杂得多。编写自己的 JSON 解析器既简单又有趣,但编写 XML 解析器是一项艰巨的任务(对于 XML 新手来说肯定不是这样)。在 Javascript 中,可以通过评估文本来解析 JSON(虽然这非常不安全)。
If you need to transfer data between two systems, JSON is fine for this. If you want to use more advanced properties from XML (entities, automatic inclusion of other documents, schema, implicit attribute values, etc.), or mix content and markup, XML will work better for you.
如果您需要在两个系统之间传输数据,那么 JSON 就可以了。如果您想使用 XML 中更高级的属性(实体、其他文档的自动包含、模式、隐式属性值等),或者混合内容和标记,XML 将更适合您。
回答by Dan Rosenstark
One thing to consider is that, at least on download, the difference in size between XML and JSON (XML being bigger) is not really interesting because you will using GZIP compression over HTTP.
需要考虑的一件事是,至少在下载时,XML 和 JSON(XML 更大)之间的大小差异并不是很有趣,因为您将通过 HTTP 使用 GZIP 压缩。
If you are sure that you will working with Javascript and never have any other type of client (another website, a Flash/Flex client, whatever) then JSON is great and perhaps a bit less painless. On the other hand, doing XML now saves you time should you ever need to get stuff working over XML.
如果您确定您将使用 Javascript 并且从来没有任何其他类型的客户端(另一个网站、Flash/Flex 客户端,等等),那么 JSON 是很棒的,而且可能没有那么痛苦。另一方面,如果您需要通过 XML 处理内容,现在使用 XML 可以节省您的时间。
Anyway, a good thing to do is check what the Google guys are doing using LiveHTTPHeaders for Gmail or any of their apps. I'm pretty sure that it's XML and not JSON, but you can check me on that. In all cases, doing what Google does might be considered to be "not thinking," but they're generally smarter than me and have many more person-hours to think anyway :)
无论如何,一件好事是检查 Google 的人正在使用 LiveHTTPHeaders for Gmail 或他们的任何应用程序在做什么。我很确定它是 XML 而不是 JSON,但你可以检查我。在所有情况下,做谷歌所做的事情可能被认为是“不思考”,但他们通常比我更聪明,而且有更多的人时间去思考:)
回答by Martin K.
It depends.
这取决于。
Example: You want to place your application into a big context (EAI), then you should use XML (maybe Webservices & WSDL).
示例:您希望将应用程序置于大上下文 (EAI) 中,那么您应该使用 XML(可能是 Webservices 和 WSDL)。
Example2: If you want a simple user interface, based on javascript and ajax you should consider JSON.
示例 2:如果您想要一个基于 javascript 和 ajax 的简单用户界面,您应该考虑 JSON。
JSON is most useful when you work with javascript. In the most cases XML libraries are so easy to use, that the ease of use is nearly the same.
当您使用 javascript 时,JSON 最有用。在大多数情况下,XML 库非常易于使用,以至于易用性几乎相同。
回答by Andrew G. Johnson
回答by Dave Archer
If you've not used either, then go for JSON. The answers so far are correct in that there is a lot of help for both and you'll get up and running on either fairly quickly. JSON has the added advantage of being less bloated than XML and can easily be 'translated' to javascript when on the client
如果您还没有使用过,那么请使用 JSON。到目前为止的答案是正确的,因为两者都有很多帮助,而且您将很快启动并运行其中任何一个。JSON 具有比 XML 更少臃肿的额外优势,并且可以在客户端轻松“翻译”为 javascript
回答by James Skidmore
I use JSON with PHP because of how simple it is:
我将 JSON 与 PHP 结合使用,因为它非常简单:
$arr = array('foo' => 'bar', 'animals' => array('cat', 'dog')); // Create any array.
$json = json_encode($arr); // Converts any array to JSON.
回答by Tarnschaf
The easiest one to start is XML. The main reason for this is not parsing (for there are very good libs for XML and JSON that do this for you, see other posts), but the understandability:
最容易开始的是 XML。这样做的主要原因不是解析(因为有非常好的 XML 和 JSON 库可以为您执行此操作,请参阅其他帖子),而是可理解性:
JSON works with a lot of different parentheses, when looking at your XML data you will probably see any errors faster. (Assuming you know e.g. HTML)
JSON 使用许多不同的括号,在查看 XML 数据时,您可能会更快地看到任何错误。(假设您知道例如 HTML)
It is also possible (but optional) to create an XML schema, that makes verifying your data automatically easy. This can save you a lot of time afterwards!
也可以(但可选)创建一个XML 模式,这使得自动验证您的数据变得容易。这可以为您节省很多事后的时间!

