XML和JSON-优缺点? [英] XML and JSON -- Advantages and Disadvantages?

查看:123
本文介绍了XML和JSON-优缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近听说过JavaScript对象表示法(JSON),并且在查找它之后,它似乎已成为可扩展标记语言(XML)的替代品而变得相当流行.

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 Language (XML).

我在页面上获取了更多信息,但它似乎更多是XML扑朔迷离的页面,而不是比较页面.所以我想我应该在这里问:

I went on this page for more info, but it seemed more of an XML-bashing page rather than a comparison page. So I thought I should ask here:

与XML相比,JSON有什么好处?为什么(如果有的话)我们应该选择一个呢?

What are the benefits of JSON as compared to XML, and why (if at all) should we choose one over the other?

推荐答案

  • JSON更紧凑,可以轻松地在JavaScript中加载.
  • XML更严格,并且支持模式和名称空间.
  • 从表面上看,JSON似乎在各方面都具有优势-灵活,紧凑且在许多情况下更易于使用(尤其是在使用JavaScript时),但是它缺少一些关键功能,尤其是:

    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:

    • 架构支持

    即甲方可以指定文档的格式,乙方可以检查他们是否提供了与此格式匹配的文件.

    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).

    • 命名空间支持

    即能够在同一文档中混合旨在由多个来源读取(或由多个来源写入)的数据.

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

    SOAP 协议就是一个有效的例子-名称空间允许将SOAP信封"分开,或与序列化的应用程序数据一起传递的包装器"数据.这允许Web框架处理和处理SOAP信封,然后将正文/有效负载数据传递到应用程序上.

    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.

    当开发需要快速,紧凑和方便的数据序列化的Web应用程序时,JSON非常有用,但是它的灵活性本质使得它不如XML适合在单独的系统之间传输数据或存储数据.将由第三方阅读.

    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.

    也许这些类型的功能会及时出现在JSON中,但就目前而言,XML是诸如Web服务和文件格式之类的主要格式.

    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.

    这篇关于XML和JSON-优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆