Could you please tell me, what is the use of JSON (in Javascript and PHP). when we need a JSON method.
I read from the following link but, i didn't get any information regarding the JSON implementation on any project.
Could you please tell me, what is the use of JSON (in Javascript and PHP). when we need a JSON method.
I read from the following link but, i didn't get any information regarding the JSON implementation on any project.
JSON is a light-weight data-interchange format (think of it as XML on a diet). Basically any place where you can use XML for serializing data you can use JSON instead.
I would suggest using JSON if you have the need of manipulating the retrieved data (i.e. through an Ajax call in your browser) with JavaScript code. In such a case JSON is very comfortable since you can directly load it into your JavaScript and use it (therefore Java Script Object Notation => JSON). This is called deserialization of a JSON string in JavaScript objects. It can be done by using eval(), which however - I read - poses some security issues, wherefore some JSON (de)serializer should be used.
As described on the page you mentioned you have some JSON string like
which is sent over the network, initiated by some of Ajax call on the client where then a callback is invoked like (i.e. in .Net)
The advantage is just the easy (de)serialization together when using JavaScript, mostly on web development with Ajax. Otherwise I'd not really use JSON but rather rely on XML since for desktop apps there are really powerful parsing libraries.
JSON is data format used in the transmission of data. It's used primarily in Javascript AJAX calls.
JSON's structure is simply bracketed name:value pairs. Because of it's compact nature and simplicity it's a better structure for the transmission of relatively small datasets and things that can be grouped into name:value pairs.
For example:
A prototype.js Ajax call transforming and receiving JSON data:
And using PHP you'd handle the request with something like this:
Something like:
will get passed around in the http request/response. Data in this format is called JSON.
JSON is just a data format. If you need to store or transport data that is no more complicated than a nested series of name-value pairs, whose values are supported by the JSON standard, then JSON might be the right data format for your project.
If your project had data storage/transport needs ;)
JSON is relatively light-weight data exchange format (at least when compared to XML or HTML) and is most useful when exchanging small amounts of data between a web client and a web server/service.
However, it is not the best choice (although much better than XML) for exchanging large lists of data, due to its overhead per row exchanged.
JSON is a great format for passing data back and forth between Javascript and PHP. My most common use is for status messages.
Here's some Javascript for doing a Ajax query to a small PHP script.
Then, on the PHP side, you can have something like this:
The neat thing is that Prototype automatically decodes the X-JSON header that PHP is creating.
The end result is that the jsonHeader argument of the onSuccess method in Javascript is automatically converted into an array that's the same data as the $jsonHeader array in PHP