Why is Everyone Choosing JSON Over XML for jQuery?

2020-01-27 09:38发布

I thought XML is highly portable and can be used as a mini database. I have seen XML used everywhere. I even see large companies switching over to JSON. Even Microsoft has integrated support for JSON. What is all the hype over JSON?

标签: jquery xml json
19条回答
乱世女痞
2楼-- · 2020-01-27 10:14

JSON is best for consumption of data in web applications from webservices for its size and ease of use, especially due to the built-in support in JavaScript. Imagine the computation overhead for parsing an xml fragment compared to the instant lookup in JSON.

A very good example is JSON-P. You can get back data from a webservice wrapped in a callback function call, like my_callback({"color": "blue", "shape":"square"}); inside a dynamically generated <script> tag so the data can be directly consumed in the function my_callback(). There is no way to get even close to this convenience using XML.

XML would be the format of choice for large documents, where you have a framework of rendering pages of data in multiple formats using XSLT. XML can also be used with application configuration files for readability among many other uses.

查看更多
爷的心禁止访问
3楼-- · 2020-01-27 10:14

XML is bloated snake oil in most situations. JSON gives you most of the benefits without the bloat.

查看更多
够拽才男人
4楼-- · 2020-01-27 10:17

Easy consumption by JavaScript can be one of the reasons ..

查看更多
Fickle 薄情
5楼-- · 2020-01-27 10:19

No one here has mentioned XML-s main advantage: validation rules (DTD, XSD). My conclusions, having used both:

  • JSON is perfect for ajax, especially if you develop both server and client side yourself. You basically create js objects right in your server script!
  • XML shines in corporate environments, when you have to set data exchange standards between big bureaucratic organizations. Often, one party develops its part months before another, so it really benefits from validating its requests against agreed XSD. Also, in big corporations, data transfer is often translated between different systems. This is also XML's strength, think XSLT. Example: code-free conversion into JSON :p

Of course, there's json-schema being developed but you won't find built-in support for it anywhere.

I'm a fanboy of both, they have just different strengths.

查看更多
Luminary・发光体
6楼-- · 2020-01-27 10:19

JSON is effectively serialized JavaScript in that you can eval(aJsonString) directly into a JavaScript object. Inside of a browser it's a no-brainer JSON is perfectly suited for JavaScript. At the same time JavaScript is a very loosely-typed dynamic language and cannot natively take advantage of all the specific type information available contained within an Xml/Xsd document. This extra metadata (which is great for interoperability) is a hinderance in JavaScript making it more tedious and cubersome to work with.

Size vs Performance

If you're in a browser JSON is faster to serialize/deserialize as it's simpler, more compact and more importantly natively supported. I have some northwind database benchmarks available comparing the size and speed between the different serializers available. In the Base Class Library Microsoft's XML DataContract serializer is over 30% faster than their JSON one. Although this has more to do with the effort Microsoft put into their XML serializer as I was able to develop a JsonSerializer that is more than 2.6x faster than their XML one. As for payloads based on the benchmarks it looks as though XML is roughly more than 2x the size of JSON. However this can quickly blow out if your XML payload uses many different namespaces within the same document.

查看更多
够拽才男人
7楼-- · 2020-01-27 10:24

It's lightweight compared to XML. If you need to scale, reduce your bandwidth requirements!

Compare JSON

 [
      {
           color: "red",
           value: "#f00"
      },
      {
           color: "green",
           value: "#0f0"
      },
      {
           color: "blue",
           value: "#00f"
      },
      {
           color: "cyan",
           value: "#0ff"
      },
      {
           color: "magenta",
           value: "#f0f"
      },
      {
           color: "yellow",
           value: "#ff0"
      },
      {
           color: "black",
           value: "#000"
      }
 ]

to XML:

 <colors>
      <color >
           <name>red</name>
           <value>#f00</value>
      </color>
      <color >
           <name>green</name>
           <value>#0f0</value>
      </color>
      <color >
           <name>blue</name>
           <value>#00f</value>
      </color>
      <color >
           <name>cyan</name>
           <value>#0ff</value>
      </color>
      <color >
           <name>magenta</name>
           <value>#f0f</value>
      </color>
      <color >
           <name>yellow</name>
           <value>#ff0</value>
      </color>
      <color >
           <name>black</name>
           <value>#000</value>
      </color>
 </colors>
查看更多
登录 后发表回答