JSON
From MapbenderWiki
Java Script Object Notation
Contents |
general
- Douglas Crockford on JSON
- a validator: http://www.jsonlint.com/
PHP
PHP 5.2.1 natively supports JSON (see JSON at php.net)
PHP-JSON
- Mapbender now (March 2007) uses JSON-PHP to support previous PHP versions.
- download
- Example: generate JSON string from object
//requires JSON library
require_once(dirname(__FILE__)."/../extensions/JSON.php");
//generate a PHP object, f.e.
$resultObj = array("id" => $row["wms_id"], "title" => $row["wms_title"], "abstract" => $row["wms_abstract"]);
//convert to JSON string
$json = new Services_JSON();
$output = $json->encode($resultObj);
header('Content-type: application/x-json');
echo $output;
- Example: generate object from JSON string
//requires JSON library require_once(dirname(__FILE__)."/../extensions/JSON.php"); $object = $json->decode($jsonString); print_r($object);
JavaScript
Example
var someObject = {};
..
// construct some object
..
// get the JSON string representation
someObject.toJSONString();
jQuery
The Library above rewritten for jQuery:
Example
var someObject = {};
..
// construct some object
..
// get the JSON string representation
$.toJSON(someObject);
AJAX
jQuery
$.getJSON() sends an Ajax request via GET and returns a JavaScript object.
Example
/php/mod_gazetteerMetadata.php
/php/mod_gazetteerMetadata_search.php
