Quercus JSON
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Tutorials

Hello World
Java Modules
JSON
Java Modules
Tutorials
Servlets and Filters

Find this tutorial in: /usr/local/resin/webapps/resin-doc/quercus/tutorial/json
Try the Tutorial

JSON (JavaScript Object Notation) is a popular text data exchange format with built-in support from Quercus since Resin 3.0.20. One of the common uses of JSON in a PHP environment is for the server to send JSON data to the user's browser. Because the JSON language is a subset of JavaScript, JSON-encoded text can be readily parsed on the user's browser using JavaScript's eval() function.

  1. Files in this tutorial
  2. Using JSON in Quercus
  3. Examples
    1. json_encode
    2. json_decode
    3. Simple Web Example
    4. AJAX Example
      1. json.php:
      2. json.html:
  4. External Links

Files in this tutorial

json.html The JSON example page
json.php The JSON PHP page

Using JSON in Quercus

Quercus has built-in JSON support and JSON functionality is enabled the moment Quercus is started: no additional downloads are required. Quercus sports two PHP functions for working with JSON: json_encode and json_decode.

  json_encode(mixed php_object)
        encodes any PHP array or object into JSON.
      
  json_decode(string json_string [, bool is_assoc])
        decodes JSON into a PHP array or object.

json_decode may return either a PHP array or object depending on the circumstances:

  • If the text is that of a JSON array[1], decoding returns a non-associative PHP array.
  • If the text is that of a JSON object and the second argument to json_decode is not specified or is false, decoding returns a standard PHP object.
  • If the text is that of a JSON object and the second argument to json_decode is true, then decoding returns an associative PHP array.

Examples

json_encode

To encode an array into JSON:

 <?php
   $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");
   $json = json_encode($array);
 ?>

The value of $json would be: '{"a":"Caucho", "b":"Resin", "c":"Quercus"}'. The JSON text may then be sent and used on the user's browser or to any client that can decode JSON.

json_decode

To decode JSON data into a standard PHP object in Quercus (using the above JSON text $json as an example):

 <?php
  $object = json_decode($json);
 ?>

$object would be a standard PHP object with three fields "a", "b", and "c" with values "Caucho", "Resin", and "Quercus" respectively.

Simple Web Example

Below is a simple example using JSON on the web.

 <script type="text/javascript">
 
   <?php
     $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");
     $json = json_encode($array);
     echo "var data = $json;";
   ?>
   
   var decoded = eval("(" + data + ")");
   
   //Should output: "Quercus at work."
   document.write(decoded.c + " at work.");
 </script>

AJAX Example

JSON data is more commonly sent to the browser via AJAX requests. Suppose there are two files defined below. The PHP script in json.php encodes an array into JSON. When the user's browser is directed to json.html, an AJAX request receives JSON data from json.php. Then the browser calls eval() on the JSON data to recover a JavaScript object.

json.php:

 <?php
   $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");
   $json = json_encode($array);
   echo $json;
 ?>

json.html:

 <html>
 <head>
 <script type="text/javascript">
  
  var url = "data.php";
  function request() {  
    if (window.XMLHttpRequest)
     http_request = new XMLHttpRequest();
    else
     http_request = new ActiveXObject("Microsoft.XMLHTTP");
    http_request.onreadystatechange = function() {
        handle_json(http_request)
      };
    http_request.open("GET", url, true);
    http_request.send(null);
  }
  
  function handle_json(http_request) {
   if (http_request.readyState == 4) {
    document.firstForm.json.value = http_request.responseText;
    var decoded = eval("(" + http_request.responseText + ")");
    document.firstForm.decoded.value = decoded.a + "'s " +
        decoded.b + " with " + decoded.c + " at work.";
   }
  }
  
  function clearForm() {
   document.firstForm.json.value = "";
   document.firstForm.decoded.value = "";
  }
 </script>
 </head>
 <body>
  <form name="firstForm">
   <p>JSON:<br><textarea name="json" cols="50"></textarea></p>
   <p>Decoded:<br><textarea name="decoded" cols="50"></textarea></p>
   <input type="button" onclick="request()" value="AJAX Request">
   <input type="button" onclick="clearForm()" value="Clear">
  </form>
 </body>
 </html>

External Links

Try the Tutorial


Java Modules
Tutorials
Servlets and Filters
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.