views:

1241

answers:

4

Why JSON? I have done some tests today, and the request time for both JSON, or a normal AJAX request was the same. In the "normal request" I have returned the complete text+html tags, in the JSON request, logically I returned a "json return type" and I have created the HTML with client-side JavaScript.

I don't get it, why are the big sites (Google Reader etc), or even small sites using JSON? Or do I not understand when I should use JSON?

+28  A: 

You are perhaps a little confused.

JSON and AJAX are not an either-or choice.

JSON and XML is a choice.

JSON and AJAX are distinct and largely unrelated, although AJAX often uses JSON, it can just easily use XML, HTML or plain text.

Or are you referring to the X in AJAX (XML)? If so, the arguments for JSON are basically:

  • JSON has a smaller payload than equivalent XML; and
  • JSON is easier to deal with in Javascript (compare eval'ing a JSON object to walking an XML fragment).

Other than that, it's largely personal preference.

cletus
Add to that, json also helps you debug if you are using firebug, since you can just read the data sent back and forth directly
Anders Rune Jensen
http://izlooite.blogspot.com/2010/05/ajax-or-ajax-xml-vs-json.html
KMan
+8  A: 

JSON is just a data-interchange format. It describes in what way the data is transfered. You can not replace Ajax with JSON.

Ajax stands for Asynchronous JavaScript and XML, but when using JSON you could say that you're using AJAJ (Asynchronous JavaScript and JSON).

Maybe you are thinking of the jQuery methods $.getJSON() and $.get()?

The difference is that $.getJSON() automatically assumes that it's JSON data, while $.get() will just fetch the data as plain text.

When using $.getJSON() you're also able to fetch data between domains.

alexn
I like to think of AJAX as standing for Asynchronous JavaScript and X, where X is some endoding format.
Ryan Michela
+2  A: 

See When to use Ajax vs Json for Javascript events? Almost similar.

Kirtan
A: 

THx guys!I was just confused

Uffo
Please do not post comment as new answer.
Török Gábor