问题描述
我想知道哪种更快: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:
edit:我应该提一下,这种比较实际上是从在具有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模式很好地消除了数据结构的歧义)-但是,在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");
原始:
这令人产生误解:请记住,在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比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!