JSON (JavaScript Object Notation) is a lightweight data-interchange format.
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
Area of use:Sending and Fetching AJAX request and response.
Advantage over XMLResponse Object: JSON doesn’t need a bit extra code to be manipulated by JavaScript on Client side, while manipulation of XML objects need minimum 4kb of script. As the number of browser in compatibility issue increases the size of script increases.
Sample PHP code:
$arr = array (’a'=>1,’b'=>2,’c'=>3,’d'=>4,’e'=>5);
echo json_encode($arr); // encode array to JSON string
Output:
{”a”:1,”b”:2,”c”:3,”d”:4,”e”:5}
Decode:
$json = ‘{”a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
json_decode($json);
For further ref: JSON
CODE
function jsonRun(){
var myFirstJSON = eval({’myname’: ‘nirbhab’, ‘myfrnd’: ‘ankesh’});
var x;
for (x in myFirstJSON)
{
document.writeln(myFirstJSON[x]);
}
}
NOTE: Some servers doesn’t have json_encode() and json_decode functionality.
You can also use PEAR Service_JSON Class instead.
PEAR
Refer URL for advance Learning on JSON : http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)