1

它只是内置工具吗?从根本上说,该格式不会妨碍自动编组 xml。

xml 和 json 之间的唯一区别是 [tag]HI World[/tag] 到 {tag:HI World},分别..所以 JSON obj 是大小的一半

正确的?

4

3 回答 3

1

JSON has its roots in Javascript, which is a dynamic language with loose typing. Every data item is "loosely typed", which means the runtime system doesn't much care whether the thing is a string of characters, or an array, or an integer, or an object or a function. Variables hold "something", and the type is determined when you try to do something with them.

Therefore de-serializing a JSON packet is pretty simple. The representation to be deserialized is either a string or a number.

With XML on the other hand, the XML Schema standard set out a number of specific data types, including basic things like integers, floats, strings, but also dateTime, date, and then more complex derived types, like ordered sequences of those things, arrays of those things, and "unions". When this XML stuff was being defined, there was a great deal of effort around guaranteeing that xml documents were a.) well formed, and b.) valid according to one of these fairly specific schema definitions. Most xml serializers use some portion of the typing magic.

In XML schema you can also specify references, so that one item in an XML document can refer to another item.

As an illustration, right now when I use the stackexchange API to query a list of "question answers" a particular user has posted, I get back a list of items, each of which contains a repeated block for the same user. They all look like this (pseudo code):

{ answers: [ 
  { answer_id: 98393398398
    question_id 28282828
    owner: {
     name: Cheeso
     user_id 48082
     reputation: 3093
     date_created: 110101010}
   },
  { answer_id: 28783398398
    question_id 111128828
    owner: {
     name: Cheeso
     user_id 48082
     reputation: 3093
     date_created: 110101010}
   },
   ...
] } 

But XML allows me to refer to other places in the document, so I'd get back something like this (again, pseudo code):

{ users: [ ["#user1", "Cheeso", "48082", 3093 ]] ,
  answers: [ 
   { answer_id: 98393398398
    question_id 28282828
    owner: #user1 },
  { answer_id: 28783398398
    question_id 111128828
    owner: #user1},
   ...
] } 

Of course you could structure your json applications to understand and use "in-document references". But the point is, with XML, that is already part of the model.

All of which means serializing and de-serializing XML can be more involved, in practice, than doing the same with JSON.

It's my opinion that people are finding the looser json approach to be preferable because it is more flexible and adaptable, and easier to use. On the other hand some documents are large enough that the rigidity and formality of XML schema is super valuable, and json is just too loose. I'm thinking of an OOXML document, for example.

于 2012-06-15T19:02:01.840 回答
1

JSON 提供了 Javascript 中使用的数据结构的序列化,这与许多编程语言中的数据结构非常相似。XML 是为文档标记而设计的。因此 JSON 旨在匹配编程语言擅长的领域,XML 旨在满足文档创作的需求。

人们发现更宽松的 json 方法更可取,因为它更灵活

相反。人们发现 JSON 更可取,因为它不太灵活。它只做像 Javascript 这样的语言很容易处理的事情。

于 2012-06-16T13:21:30.840 回答
1

这不一定更容易。一个完整的 JSON 解析器实现可能与 XML 文档的实现一样复杂。诚然,由于显而易见的原因,JSON 字符串的大小几乎总是小于其等效的 XML。

但是,在 XML 中的 JSON 作为数据传输格式之间进行选择取决于底层技术(JavaScript 到 JavaScript 或服务器,或相反)。在其他情况下,例如当您的数据已经作为 XML 存在时,将其转换为 JSON 以便您可以将其发送到某个地方(到 REST 服务或其他任何地方)并不是很有效。

因此,这是一个简单的选择:选择您正在使用的技术最能支持的内容。JSON 用于处理 JavaScript 和 XML 用于 SOAP Web 服务或当您的数据已经在 XML 中时。

于 2012-06-15T18:43:02.587 回答