views:

117

answers:

6

I know there are a lot of sources which explain this, but they just talk about storing key/value pairs in an array or in an object.

I can't understand why or when should I do this, although I always use jquery's AJAX. As an example, if I make a forum when do I need this? Can someone give real world examples?

The same goes for XML. It stores information too. But what kind of information? Why can't I store it in MySQL?

EDIT: so with $.json() you retrieve a json file and then display the content in the browser. but who creates this json file? me? manually? and why? and how?

so you can just retrieve a json file? can you do something else?

+5  A: 

Basically, both JSon and XML are used to store structured data. If your AJAX call only gets a single value (like "number of votes"), you could just fetch it and not use JSON or XML at all. But as soon as you need to fetch several values (like "positive votes" and "negative votes"), you need some structure.

Both JSON and XML support nesting data (like a.b.c=X) . JSON has the advantage of being lightweight and very very easy to parse in JS (as, basically, JSON is JS), which is why it is generally preferred over XML for AJAX applications.

Even in the very simple "only one value" case, I'd recommend directly using JSON as it provides room for easy extensibility, whereas a single value would not.

Zorglub
when you mean ajax call do you mean the php file that is called using ajax? so json is for sending over information to the phpfile?how does the php file retrieve that information? when i send values over with jquery ajax it looks like this: $.post("static/js/ajaxcall_register.php", {username: $("#register_box #username").val(), email: $("#register_box #email").val(), password: $("#register_box #password").val()}, function(data) { $("#register_box #error").html(data); });the php file fetches the values with $_POST['value']; how does the php fetch json key/values?
never_had_a_name
using some json parser.
Amarghosh
@fayer: JSON is generally used on the other direction. In your example, the PHP file would send the requested data back in JSON.
Martinho Fernandes
but shouldnt i use $.getJSON then? but if i use $.getJSON then I will not be able to call the php file? :Si send back data from php file using echo. how can i send back data in json?...oh god...i cant get it...plz give me a real example or a guide to it.
never_had_a_name
You can send JSON with echo. You just write a JSON string, something like echo('{"answer":42}')
Martinho Fernandes
A: 

JSON and XML are both formats typically used for conveying information, in addition to storing it (though XML documents are often used for storing information as well, and JSON could theoretically always be written to a text file).

Amber
A: 

You probably are storing the information in SQL at the back end.

The purpose of JSON/XML in this context is to provide data to client-side Javascript through a webservice, which at the back end is probably a server-side language (php, ruby, whatever) that is pulling data from SQL, and then marshaling it into JSON/XML for consumption by the requesting client.

phoebus
A: 

Take a look at Why JSON isn’t just for JavaScript

S.Mark
A: 

You store data into mysql (or any database in general) at the server side. json and xml are the formats used to send data from client to the server and back.

Amarghosh
A: 

I tend to use JSON when working in web applications because it is a lot smaller than XML because it doesnt require the bloat that the nodes create. With the Native support for JSON being added to browsers more and more people are starting to use it more than XML.

AutomatedTester
What do you mean, native support for JSON? Any browser with JavaScript has native support for JSON: JSON is JavaScript.
Martinho Fernandes
JSON is not JavaScript, its a way of representing data as a object for JavaScript to use.JSON is a text format that is completely language independent.Native support for JSON means that you don't have to use Eval() on a string to get the JSON object out so that you can using it like a hash. Native support for JSON was only put in Firefox 3.5.
AutomatedTester
Oh, wasn't aware that we were finally moving away from dangerous eval() or dedicated JS libraries. Thanks. And btw, IE8 also has native support.
Martinho Fernandes