Read more about this book |
In this article, by Vijay Joshi, author of PHP jQuery Cookbook, we will cover:
(For more resources on this subject, see here.)
Recently, JSON (JavaScript Object Notation) has become a very popular data interchange format with more and more developers opting for it over XML. Even many web services nowadays provide JSON as the default output format.
JSON is a text format that is programming-language independent and is a native data form of JavaScript. It is lighter and faster than XML because it needs less markup compared to XML.
Because JSON is the native data form of JavaScript, it can be used on the client side in an AJAX application more easily than XML.
A JSON object starts with { and ends with }. According to the JSON specification, the following types are allowed in JSON:
A JSON can be as simple as:
{
"name":"Superman", "address": "anywhere"
}
An example using an array is as follows:
{
"name": "Superman", "phoneNumbers": ["8010367150", "9898989898",
"1234567890" ]
}
A more complex example that demonstrates the use of objects, arrays, and values is as follows:
{
"people":
[
{
"name": "Vijay Joshi",
"age": 28,
"isAdult": true
},
{
"name": "Charles Simms",
"age": 13,
"isAdult": false
}
]
}
An important point to note:
{
'name': 'Superman', 'address': 'anywhere'
}
Above is a valid JavaScript object but not a valid JSON. JSON requires that the name and value must be enclosed in double quotes; single quotes are not allowed.
Another important thing is to remember the proper charset of data.
Remember that JSON expects the data to be UTF-8 whereas PHP adheres to ISO-8859-1 encoding by default.
Also note that JSON is not a JavaScript; it is basically a specification or a subset derived from JavaScript.
Now that we are familiar with JSON, let us proceed towards the recipes where we will learn how we can use JSON along with PHP and jQuery.
Create a new folder and name it as Chapter 4. We will put all the recipes of this article together in this folder. Also put the jquery.js file inside this folder.
To be able to use PHP's built-in JSON functions, you should have PHP version 5.2 or higher installed.
This recipe will explain how JSON can be created from PHP arrays and objects
Create a new folder inside the Chapter4 directory and name it as Recipe1.
<?php
$travelDetails = array(
'origin' => 'Delhi',
'destination' => 'London',
'passengers' => array
(
array('name' => 'Mr. Perry Mason', 'type' => 'Adult',
'age'=> 28),
array('name' => 'Miss Irene Adler', 'type' => 'Adult',
'age'=> 28)
),
'travelDate' => '17-Dec-2010'
);
echo json_encode($travelDetails);
?>
{
"origin":"Delhi",
"destination":"London",
"passengers":
[
{
"name":"Mr. Perry Mason",
"type":"Adult",
"age":28
},
{
"name":"Miss Irene Adler",
"type":"Adult",
"age":28
}
],
"travelDate":"17-Dec-2010"
}
PHP provides the function json_encode() to create JSON strings from objects and arrays. This function accepts two parameters. First is the value to be encoded and the second parameter includes options that control how certain special characters are encoded. This parameter is optional.
In the previous code we created a somewhat complex associative array that contains travel information of two passengers. Passing this array to json_encode() creates a JSON string.
Any of the following constants can be passed as a second parameter to json_encode().
These constants require PHP version 5.3 or higher.