XML and JSON — Advantages and Disadvantages?

前端 未结 8 650
遥遥无期
遥遥无期 2020-12-22 22:12

I recently heard about JavaScript Object Notation (JSON), and after looking it up, it seems like it\'s becoming rather popular as an alternative to the Extensible Markup Lan

相关标签:
8条回答
  • 2020-12-22 22:31

    Advantages of JSON

    • Smaller message size
    • 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

    Draw

    • Human Readable
    • Easy to parse
    0 讨论(0)
  • 2020-12-22 22:32

    XML

    • Can have a schema that states its format.
      • This is of interest to quality control people. You can prove that its format matches what is expected, and therefore you may not have to be quite as fervent as you might otherwise be at checking that a field exists within it every time you want to reference one.
      • (Though this pre-supposes that you go out of your way to actually validate the XML against its schema.)
    • Bloated; each field name has to be written out twice per field. Ew!

    JSON

    • Far less bloated, easier to parse and arguably more human readable (if you space it out properly).
    • Not quite as powerful: not expressive enough to separate attributes from values.
    0 讨论(0)
  • 2020-12-22 22:33

    JSON - smaller and can be natively loaded as JavaScript object (speed is a value)

    XML - still standard, however older (slower, bigger, but not only JS)

    0 讨论(0)
  • 2020-12-22 22:46

    Less verbose- XML uses more words than necessary

    JSON is faster- Parsing XML software is slow and cumbersome. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.

    JSON data model’s structure matches the data: JSON’s data structure is a map whereas XML is a tree. Although a map (just key/value pairs) can be limiting, that’s what we want, because it is easier to interpret and is predictable.

    In code: Items are represented the same way in code. In many languages, especially dynamic ones, you can just ‘slurp in the JSON’ and you immediately have your domain object. It is easy to go from objects in JSON to the objects in code because they align. When going from objects in XML to objects in code they do not align and there is a lot of room for interpretation.

    JSON is limiting, but that’s a good thing: JSON is limited in terms of what objects can be modeled. Some may think XML is better because more objects can be modeled and it doesn’t prohibit developers. But even though JSON prohibits developers, it is in a positive way, making the code simpler, more predictable, and easy to read. XML can be formatted to look and function any way a company wants, but it makes it difficult for developers to read, understand, and convert. In most cases people believe XML is better because developers can do anything under the sun but in the age of simplifying, less is more, making JSON a better alternative.

    0 讨论(0)
  • 2020-12-22 22:48
    • JSON is more compact and can be easily loaded in JavaScript.
    • XML is stricter and has support for schemas and namespaces.

    On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:

    • Schema support,

    I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.

    This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).

    • Namespace support,

    I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.

    An example of this in action is the SOAP protocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.


    JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.

    Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.

    0 讨论(0)
  • 2020-12-22 22:48

    Advantages of XML

    1. Near ubiquitous support in a wide array of languages and frameworks. More likely than not there's already a tool out there to help your extract information from an XML response.

    2. It can adhere to a concrete schema if so you choose. Once it validates, you can say it's correct and start parsing.

    3. Namespaces allow you to divide the XML.

    Advantages of JSON

    1. Lightweight in comparison to XML. Fewer characters = smaller time going through the internet tubes.

    2. Easier to handle with Javascript if you need something for a web application.

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