As you know by now, Ajax has to do with the interaction between the client and the server, so JSON would be of no use for this purpose unless there were server-side tools to aid in the encoding and decoding. As luck would have it, there are quite a few JSON utilities for server-side languages. Although it is beyond the scope of this book to discuss every one of these tools, it is useful to take a look at one and then develop a solution using it.
JSON-PHP is a PHP utility to ease the encoding and decoding of JSON information. This utility, written by Michal Migurski, is available for free at http://mike.teczno.com/json.html. All you need to begin using JSON in PHP is to include the JSON.php file in your page and make use of the JSON object.
Creating new instance of the JSON object is quite simple:
<?php require_once("JSON.php"); $oJSON = new JSON(); ?>
The first line includes the JSON.php file that contains the JSON object definition. The second line simply instantiates the object and stores it in the variable $oJSON. Now you're ready to start encoding and decoding JSON in your PHP page.
To encode a PHP object into a JSON string, use the encode() method, which accepts a single argument: an object to encode, which can be an array or a full-fledged object. It doesn't matter how the object or array was created, whether using a class definition or not; all objects can be encoded using this method. Consider the following class definition:
<?php class Person { var $age; var $hairColor; var $name; var $siblingNames; function Person($name, $age, $hairColor) { $this->name = $name; $this->age = $age; $this->hairColor = $hairColor; $this->siblingNames = array(); } } ?>
This PHP code defines a class called Person that stores some personal information. You would use this class as follows:
<?php $oPerson = new Person("Mike", 26, "brown"); $oPerson->siblingNames[0] = "Matt"; $oPerson->siblingNames[1] = "Tammy"; ?>
To encode the $oPerson object, you simply pass it into the encode() method, like this:
<?php $sJSONText = $oJSON->encode($oPerson); ?>
This creates a JSON string of:
{"age":26,"hairColor":"brown","name":"Mike","siblingNames":["Matt","Tammy"]}
The $oPerson object is now ready to be transferred to JavaScript or any other language that can support JSON-encoded information.
But what if you already have a JSON string? That's where the decode() method is used.
Suppose you have the JSON string displayed previously and want to create a PHP object from it. Just pass the string into the decode() method:
<?php $oPerson = $oJSON->decode($sJSONText); ?>
Now the $oPerson variable can be used just like the one in the previous example, as if it were created using the Person class:
<?php print("<h3>Person Information</h3>"); print("<p>Name: ".$oPerson->name."<br />"); print("Age: ".$oPerson->age."<br />"); print("Hair Color: ".$oPerson->hairColor."<br />"); print("Sibling Names:</p><ul>"); for ($i=0; $i < count($oPerson->siblingNames); $i++) { print("<li>".$oPerson->siblingNames[$i]."</li>"); } print("</ul>"); ?>
This code prints out the information contained in the $oPerson object, proving that the object has been constructed appropriately. JSON-PHP will be used in several projects throughout this book because it is quite simply the easiest way to deal with JSON in a server-side language.
You can find a number of JSON tools for server-side languages, so no matter what your preference is, you can use the power of JSON:
C#/.NET: The C# JSON library, written by Douglas Crockford, is available at www.crockford.com/JSON/cs/.
ColdFusion: The CFJSON library, written by Jehiah Czebotar, is available at http://jehiah.com/projects/cfjson/.
Java: The JSON in Java utilities, written by Douglas Crockford, are available at www.crockford.com/JSON/java/. Can be used in JSP.
Perl: The JSON library, written by Makamaka Hannyaharamitu, is available at http://search.cpan.org/dist/JSON/.
PHP: In addition to JSON-PHP, there is also php-json, a C extension for PHP written by Omar Kilani and available at www.aurore.net/projects/php-json/. You must be comfortable with compiling PHP with extensions.
Python: The json-py library, written by Patrick D. Logan, is available at https://sourceforge.net/projects/json-py/.
Douglas Crockford also maintains a fairly comprehensive list of JSON utilities at www.crockford.com/JSON/index.html. Check there before searching for JSON utilities for any other language.