JSON stands for JavaScript Object Notation. And basically looks something like this
{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
This is an alternate format to serialising data and its key benefit is that it can be converted to an object and back to a string again for data transmission in almost any language.
It is easy to inspect as it has a structure, and is a native JavaScript syntax so a simple eval() function can bring it back to life. It is common to use this instead of XML for many server requests, such as AJAX or API services.
jQuery's AJAX methods automatically convert a returned json result into an object so you can access its properties without any hassle. e.g.
$.get(this.href,{},function(json_response){<br>
alert(json_response.firstName + ' ' + json_response.lastName);<br>
});
Note how json_response is just a regular object like any other.
On the server side, we can use PHP like this to return a JSON result.
<?php
class foo{
public $firstName;
public $lastName;
}
$obj_foo = new foo();
$json_response = json_encode($obj_foo);
header('Content-type: application/json');
print $json_response;
?>
Please note however, that IE still does not recognise the application/json mime type, but you can still send it back as text/plain or load a JSON parser library for IE.
This should be enough to get you started on your way. For more information you may wish to look at these pages:
