- JSON is a valid subset of JavaScript, Python, and YAML
- JSON parsing is generally faster than XML parsing.
- JSON is a more compact format, meaning it weighs far less on the wire than the more verbose XML.
- JSON is easier to work with in some languages (such as javascript, python, and php)
- Formatted JSON is generally easier to read than formatted XML.
- JSON specifies how to represent complex datatypes, there is no single best way to represent a data structure in XML.
- JSON is a natural data structure for Javascript -- it is in fact javascript. This makes it an easy and light model for data transfer <> server/services.
- JSON is a nice format to extract XML data into. It‘s much nicer than extracting a big XML document...into a smaller XML document.
- I am going to assuming that you asking about advantages of each format in the context of an ajax response. The biggest advantage for me is that JSON ends up being much, much easier to traverse, identify, and read nodes than XML. XML methods are cumbersome and long to write, while JSON is much more cruft and simple.
- Helpful In the mobile context - it is also often less verbose than XML which helps to reduce bytes transferred.
- More structural information in the document
- Can easily distinguish between the number 1 and the string "1" as numbers, strings (and Booleans) are represented differently in JSON.
- Can easily distinguish between single items and collections of size one (using JSON arrays).
- Easier to represent a null value.
- Easily consumed by JavaScript
Advantages of XML
- Namespaces allow for sharing of standard structures
- Better representation for inheritance
- Standard ways of expressing the structure of the document: XML schema, DTD, etc
- Parsing standards: DOM, SAX, StAX
- Standards for querying: XQuery and XPath
- Standards for transforming a document: XSLT
eg.
to expand on the last point, the JSON object
{ "foo": { "bar": "baz" } }
could be represented in XML as
<foo bar="baz" .>
or
<foo><bar>baz</bar></baz>
or
<object name="foo">< property name="bar">baz</property ></object>
时间: 2024-10-14 11:45:12