When to prefer JSON over XML?

后端 未结 19 1410
无人共我
无人共我 2020-11-28 00:31

My requirement is just to display a set of values retrieved from database on a spread. I am using jquery.

相关标签:
19条回答
  • 2020-11-28 01:02

    From the first line at http://json.org/xml.html

    Extensible Markup Language (XML) is a text format derived from Standard Generalized Markup Language (SGML). Compared to SGML, XML is simple. HyperText Markup Language (HTML), by comparison, is even simpler. Even so, a good reference book on HTML is an inch thick. This is because the formatting and structuring of documents is a complicated business. . . .

    Clearly JSON is faster, but it's even more clear that it is hard to read. Use JSON for speed, use XML if there will be human-interaction and you can sacrifice the speed.

    0 讨论(0)
  • 2020-11-28 01:03

    Using JSON

    • If the data is to be consumed by JavaScript in the browser.
    • Data model is simple and not complex(too many composite objects).

    Using XML

    • Mostly in an SOA kind of environment where you are integrating several services on heterogeneous platforms and technologies.
    • SOAP has an advantage that it can be transmitted across different protocols other then HTTP.
    • Easy to use in data model transformation tool like XSLT,XSL-FO etc.
    • Lot of Database support to store/query(XQuery) XML data.
    • XML is a very mature data format so you would find plenty of tools to support any use case that you can think of.
    0 讨论(0)
  • 2020-11-28 01:03

    I found this article at digital bazaar really interesting.

    Some portions from the article are quoted below.

    About JSON pros:

    If all you want to pass around are atomic values or lists or hashes of atomic values, JSON has many of the advantages of XML: it’s straightforwardly usable over the Internet, supports a wide variety of applications, it’s easy to write programs to process JSON, it has few optional features, it’s human-legible and reasonably clear, its design is formal and concise, JSON documents are easy to create, and it uses Unicode. ...

    About XML pros:

    XML deals remarkably well with the full richness of unstructured data. I’m not worried about the future of XML at all even if its death is gleefully celebrated by a cadre of web API designers.

    And I can’t resist tucking an "I told you so!" token away in my desk. I look forward to seeing what the JSON folks do when they are asked to develop richer APIs. When they want to exchange less well strucured data, will they shoehorn it into JSON? I see occasional mentions of a schema language for JSON, will other languages follow? ...

    0 讨论(0)
  • 2020-11-28 01:04

    Considering your specific case where you're already doing javascript on the client side, I'd go with JSON for these reasons:

    • Since JSON is native to javascript you'd have to write less code on the client side - Just eval() (or, better yet, JSON.parse()) the JSON string and get an object you can use.

    • At the same time evaluating JSON on the client-side will be more efficient, and therefore faster.

    • JSON serialization produces shorter strings than XML. Using JSON will reduce the amount of data running across the wire and improve performance in that respect.

    Here's some further reading: http://www.subbu.org/blog/2006/08/json-vs-xml

    0 讨论(0)
  • 2020-11-28 01:05

    Quick rules:

    • JSON: program-to-program data format
    • YAML (JSON superset): human-to-program data format
    • XML: document markup format

    Explanation:

    JSON's sole role is to serialize object-oriented data using the data types common to most programming languages: lists, hashes, and scalars, and for that purpose it really can't be beaten or improved upon. To wit "JSON has no version number [as] no revisions to the JSON grammar are anticipated". - Douglas Crockford (Can't beat that as a sign that you do your job perfectly)

    XML was once sold as a data inter-change format, but consider the two most common use cases: Asynchronous client-server communication (AJAX) - JSON has pretty much replaced XML entirely (The X should really be a J), and web services: JSON has made XML a redundant alternative.

    The other thing XML was widely used for was human writable/readable(?) data files for programs, but here too you have a more concise, more program-friendly, more human-friendly format in YAML, a JSON superset.

    So for data representation, JSON beats XML across the board. What's left for XML then? Mixed-content document representation, which is what it was intended for.

    0 讨论(0)
  • 2020-11-28 01:07

    from JSON - the last feet

    When you go down the JSON route, you run into the same issues that XML faced 10 years ago:

    Mixing data from two different sources into one JSON packet can cause element labels to bump into each other. Mix up a packing slip and an invoice, and suddenly the From address may mean something quite different. That’s why XML has namespaces.

    Converting between different JSON structures would require writing mundane code. A more declarative way to map data would make the job easier. That’s why XML has XSLT.

    Describing a JSON packet’s structure—its fields, data types, etc.—is necessary in order for people to hook into your services. It’s essential to have a metadata language for this. That’s why XML has Schemas.

    Carrying on two simultaneous client-server conversations takes care. If you ask the server two questions and get one answer back, how do you know what question it answers? That’s why XML has WS-Correlation.

    0 讨论(0)
提交回复
热议问题