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条回答
We Are One
2楼-- · 2020-01-27 10:06

Both are great and very portable. However JSON has been gaining popularity since it serializes into less characters in most cases (which translates into a faster delivery time) and since it matches the JavaScript object syntax it can be directly translated into an in-memory object which makes Ajax a lot easier to implement.

XML is still great. JSON's just the "latest and greatest" compared to XML.

查看更多
孤傲高冷的网名
3楼-- · 2020-01-27 10:08

Easily parsed by JavaScript and it is lightweight (a document in JSON is smaller than a XML document that contain the same data.)

查看更多
Summer. ? 凉城
4楼-- · 2020-01-27 10:10

The performance of JSON isn't much different from XML for most use cases, JSON isn't well suited and readable for deeply nest structures... you will run into ]]]}], which makes debugging difficult

查看更多
混吃等死
5楼-- · 2020-01-27 10:10
 <colors>
      <color name='red'     value='#f00'/>
      <color name='green'   value='#0f0'/>
      <color name='blue'    value='#00f'/>
      <color name='cyan'    value='#0ff'/>
      <color name='magenta' value='#f0f'/>
      <color name='yellow'  value='#ff0'/>
      <color name='black'   value='#000'/>
 </colors>

With attributes, XML is nice. But for some reason, home-made XML is generally 100% made of elements, and ugly.

查看更多
劫难
6楼-- · 2020-01-27 10:11

I find that a big benefit of JSON over XML is that I don't have to decide how to format the data. As some have shown, there are numerous ways to do even simple data structures in XML -- as elements, as attribute values, etc. Then you have to document it, write up XML Schema or Relax NG or some other crap... It's a mess.

XML may have its merits, but for basic data interchange, JSON is much more compact and direct. As a Python developer, there is no impedance mismatch between the simple data types in JSON and in Python. So if I was writing a server-side handler for an AJAX query that was asking about snow conditions for a particular Ski resort, I would build up a dictionary like follows:

conditions = {
    'new_snow_24': 5.0,
    'new_snow_48': 8.5,
    'base_depth': 88.0,
    'comments': 'Deep and steep!',
    'chains_required': True,
}
return simplejson.dumps(conditions)   # Encode and dump `conditions` as a JSON string

When translated through JSON (using a library like 'simplejson' for Python), the resulting JSON structure looks nearly identical (except in JSON, booleans are lower-cased).

Decoding that structure only requires a JSON parser, whether it's for Javascript or Objective-C for a native iPhone app or C# or a Python client. The floats would get interpreted as floats, the strings as strings, and booleans as booleans. Using the 'simplejson' library in Python, a simplejson.loads(some_json_string) statement would give me back a full data structure like I just made in the above example.

If I wrote XML, I'd have to decide whether to do elements or attributes. Both of the following are valid:

<conditions>
    <new-snow-24>5</new-snow-24>
    <new-snow-48>8.5</new-snow-48>
    <chains-required>yes</chains-required>
    <comments>deep and steep!</comments>
</conditions>

<conditions newSnow24="5" newSnow48="8.5" chainsRequired="yes">
   <comments>deep and steep!</comments>
</conditions>

So not only do I have to think about the data that I may want to send to the client, I have to think about how to format it. XML, while simpler than plain SGML by being more strict with its rules, still provides too many ways to think about that data. Then I would have to go about generating it. I could not just take a Python dictionary (or other simple data structure) and say "go make thyself into my XML". I could not receive an XML document and immediately say "go make thyself into objects and data structures" without writing a custom parser, or without requiring the additional overhead of XML Schema/Relax NG and other such pains.

The short of it is that it's just much easier and much more direct to encode and decode data to JSON, especially for quick interchanges. This may apply more to people coming from a dynamic language background, as the basic data types (lists, dictionaries, etc) built in to JavaScript / JSON directly map to the same or similar data types in Python, Perl, Ruby, etc.

查看更多
一夜七次
7楼-- · 2020-01-27 10:13

Quite honestly, there isn't so much that's different between JSON and XML in the fact that they can represent all types of data. However, XML is syntactically bigger than JSON and that makes it heavier than JSON.

查看更多
登录 后发表回答