JSON

From MapbenderWiki

Jump to: navigation, search

Java Script Object Notation

Contents

general

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

Tutorial

download

Example

var someObject = {};
..
// construct some object
..
// get the JSON string representation
someObject.toJSONString();

jQuery

The Library above rewritten for jQuery:

download


Example

var someObject = {};
..
// construct some object
..
// get the JSON string representation
$.toJSON(someObject);

AJAX

jQuery

Ajax → $.getJSON()

$.getJSON() sends an Ajax request via GET and returns a JavaScript object.

Example

see gazetteerMetadata

/php/mod_gazetteerMetadata.php

/php/mod_gazetteerMetadata_search.php

Personal tools