When do I need to use JSON and XML?
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开发者_JS百科.
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?
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.
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).
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.
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.
Take a look at Why JSON isn’t just for JavaScript
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.
精彩评论