JSON 和 XML 比较 [英] JSON and XML comparison

查看:17
本文介绍了JSON 和 XML 比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪个更快:XML 和 JSON?什么时候使用哪一个?

I want to know which is faster: XML and JSON? When to use which one ?

推荐答案

在回答什么时候用哪一个之前,来一点背景:

Before answering when to use which one, a little background:

我应该提一下,这种比较实际上是从在带有 JavaScript 的浏览器中使用它们的角度来看的.这不是使用数据格式必须的方式,而且有很多很好的解析器会改变细节,使我所说的不太有效.

edit: I should mention that this comparison is really from the perspective of using them in a browser with JavaScript. It's not the way either data format has to be used, and there are plenty of good parsers which will change the details to make what I'm saying not quite valid.

JSON 更紧凑,(在我看来)更易读 - 在传输中它可以更快",因为传输的数据更少.

JSON is both more compact and (in my view) more readable - in transmission it can be "faster" simply because less data is transferred.

在解析中,这取决于您的解析器.将代码(无论是 JSON 还是 XML)转换为数据结构(如地图)的解析器可能会受益于 XML 的严格性质(XML Schemas 很好地消除了数据结构的歧义) - 但是在 JSON 中,可以在语法上推断项目的类型(字符串/数字/嵌套 JSON 对象),例如:

In parsing, it depends on your parser. A parser turning the code (be it JSON or XML) into a data structure (like a map) may benefit from the strict nature of XML (XML Schemas disambiguate the data structure nicely) - however in JSON the type of an item (String/Number/Nested JSON Object) can be inferred syntactically, e.g:

myJSON = {"age" : 12,
          "name" : "Danielle"}

解析器不需要足够聪明就可以意识到 12 代表一个数字,(而 Danielle 和其他字符串一样是一个字符串).所以在javascript中我们可以这样做:

The parser doesn't need to be intelligent enough to realise that 12 represents a number, (and Danielle is a string like any other). So in javascript we can do:

anObject = JSON.parse(myJSON);
anObject.age === 12 // True
anObject.name == "Danielle" // True
anObject.age === "12" // False

在 XML 中,我们必须执行以下操作:

In XML we'd have to do something like the following:

<person>
    <age>12</age>
    <name>Danielle</name>
</person>

(顺便说一句,这说明了 XML 更为冗长的观点;关注数据传输).要使用这些数据,我们会通过解析器运行它,然后我们必须调用类似的东西:

(as an aside, this illustrates the point that XML is rather more verbose; a concern for data transmission). To use this data, we'd run it through a parser, then we'd have to call something like:

myObject = parseThatXMLPlease();
thePeople = myObject.getChildren("person");
thePerson = thePeople[0];
thePerson.getChildren("name")[0].value() == "Danielle" // True
thePerson.getChildren("age")[0].value() == "12" // True

实际上,一个好的解析器可能会为您键入 age (另一方面,您可能不希望它这样做).当我们访问这些数据时会发生什么 - 而不是像上面的 JSON 示例那样进行属性查找 - 我们正在对键 name 进行映射查找.像这样形成 XML 可能更直观:

Actually, a good parser might well type the age for you (on the other hand, you might well not want it to). What's going on when we access this data is - instead of doing an attribute lookup like in the JSON example above - we're doing a map lookup on the key name. It might be more intuitive to form the XML like this:

<person name="Danielle" age="12" />

但我们仍然需要进行地图查找才能访问我们的数据:

But we'd still have to do map lookups to access our data:

myObject = parseThatXMLPlease();
age = myObject.getChildren("person")[0].getAttr("age");

原始:

在大多数编程语言中(并非全部,无论如何),像这样的地图查找比属性查找(就像我们在解析 JSON 时得到的那样)成本更高.

In most programming languages (not all, by any stretch) a map lookup such as this will be more costly than an attribute lookup (like we got above when we parsed the JSON).

这是一种误导:请记住,在 JavaScript(和其他动态语言)中,地图查找和字段查找之间没有区别.实际上,字段查找就是只是地图查找.

This is misleading: remember that in JavaScript (and other dynamic languages) there's no difference between a map lookup and a field lookup. In fact, a field lookup is just a map lookup.

如果您想要进行真正有价值的比较,最好对其进行基准测试 - 在您计划使用数据的上下文中进行基准测试.

If you want a really worthwhile comparison, the best is to benchmark it - do the benchmarks in the context where you plan to use the data.

在我打字的时候,Felix Kling 已经给出了一个相当简洁的答案,比较了它们的使用时间,所以我不会再继续了.

As I have been typing, Felix Kling has already put up a fairly succinct answer comparing them in terms of when to use each one, so I won't go on any further.

这篇关于JSON 和 XML 比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆