When to prefer JSON over XML?

后端 未结 19 1408
无人共我
无人共我 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 00:55

    Usually JSON is more compact, and faster to parse.

    Prefer XML if:

    • You need to process the data on the client, and you can leverage XSL for that. Chances are the XML + XSL chain will work faster than JSON + JavaScript especially for big chunks of data.
      • One good case is to convert the data into an HTML snippet.
    • Various legacy cases:
      • There is an existing XML service, and it is a hassle to rewrite it with JSON for some reasons.
      • You have to post this data back as XML after some light processing using user's input.

    One important case of (almost) XML: try to detect when sending HTML snippets is more beneficial than sending raw data. AHAH can do wonders in simple applications, yet frequently overlooked. Usually this style assumes that a server sends HTML snippets that will be inlined in the web page without processing.

    Usually in AHAH cases CSS is being leveraged to the max to massage snippets visually and implementing simple conditionals like hiding/showing relevant parts of the snippet using user-specific or application-specific settings.

    0 讨论(0)
  • 2020-11-28 00:56

    JSON is always preferable in terms of the processing the client browser has to do for parsing the data. Also, JSON is light weight data exchange format.

    XML parsing always consumes lot of browser resources and should be avoided as much as we can unless otherwise required.

    0 讨论(0)
  • 2020-11-28 00:57

    Both XML and JSON are supported by Microsoft. XML literals were the new cool feature in VB 9. In the upcoming version of ASP.NET 4.0 JSON is a must to leverage the power of client side templating.

    From the question you have asked it seems JSON might be the choice for you as it is easy to process on client side with or without jQuery.

    0 讨论(0)
  • 2020-11-28 00:58

    JSON is easy and faster to parse. XML is a little more difficult to parse, and is slower to parse and transfer (in most cases).

    Since you're using jQuery, I suggest using JSON: jQuery can retreive JSON data and convert it into a Javascript object automatically. In fact, you can convert JSON data into a Javascript object using eval. XML would have to be transversed manually by you (I don't know how this works in Javascript, but it's difficult/more annoying in most languages I've used XML libraries with).

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

    Some other things that I have run into in the XML vs JSON relm:

    JSON is very good for

    • name/value pairs
    • nesting those pairs

    Which means it tends to like an array or nested array. However JSON is missing both

    • attributes
    • namespacing

    So if you were to combine two or more JSON services there could be potential namespace conflicts. That being said JSON can be used for about 90% of the same things XML can be used for when exchanging data in my experience.

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

    Favor XML over JSON when any of these is true:

    • You need message validation
    • You're using XSLT
    • Your messages include a lot of marked-up text
    • You need to interoperate with environments that don't support JSON

    Favor JSON over XML when all of these are true:

    • Messages don't need to be validated, or validating their deserialization is simple
    • You're not transforming messages, or transforming their deserialization is simple
    • Your messages are mostly data, not marked-up text
    • The messaging endpoints have good JSON tools
    0 讨论(0)
提交回复
热议问题