Recommend this page to a friend! |
JSON | > | All threads | > | Below is the small example that uses... | > | (Un) Subscribe thread alerts |
|
eosphere rated this package as follows:
Utility: | Good |
---|---|
Consistency: | Good |
Documentation: | Insufficient |
Examples: | Insufficient |
eosphere - 2008-04-30 08:54:41
Below is the small example that uses this very good library (using a PHP4 server). This is a whole loop involving an ajax call. Please cut and paste the folling text in two files test_json.php and test_json_ajax.php (the JSON.php and json.js [from json.org] are supposed to be in a 'lib' subdirectory). Sorry for the french comments.
<?php # ***** test_json.php **** #---------------------------------- # petit programme à appeler depuis navigateur # a) déclaration d'un objet PHP, sérialisation de cet objet en string JSON # b) construit un objet javascript à partir de cette string # c) en envoie cette string en ajax vers test_json_ajax.php # d) (test_json_ajax.php désérialise la variable reçue en objet PHP, puis le resérialise et le renvoie dans la réponse Ajax) # e) on récupère la réponse (fonction ajax_retrieve) et on reconstruit un objet javascript ! require "lib/json.php"; # a) déclaration de l'objet PHP #-------------------------------- class a { var $a; var $b; var $c; function a() { $this->a = 5 / 3; $this->b = true; $this->c = range(5,100); } } class b { var $class_a; var $b; var $c; function b() { for($i=0; $i < 50; $i++) $this->class_a[] = new a; $this->b = "this is a text"; $this->c = true; } } $obj = new b; $json = new JSON; $var = $json->serialize( $obj ); // $handle = fopen("d:/tmp/debug.txt", 'w'); // fwrite($handle, $var); // fwrite($handle, "\n\n------\n\n"); // fwrite($handle, addslashes($var)); // fclose($handle); ?> <html> <head> <!-- script type="text/javascript" src="/mutu/cm/skel/lightbox/js/prototype.js"></script --> <script type="text/javascript" src="lib/json.js"></script> <script type="text/javascript" src="lib/featherajax.js"></script> </head> <body> <script> function ajax_send(str) { var XHR = new AjaxObject(); XHR.funcWait = null; XHR.funcDone = null; XHR.callBack = ajax_retrieve; XHR.appendData("jsonStr", str); XHR.sendAndLoad("test_json_ajax.php", "POST"); } function ajax_retrieve (obj) { // e) le programme de test Ajax renvoie la réponse inchangée que l'on parse en objet javascript //------------------------------------------------------------------------------------------------ var objRecuAjax = eval('(' + obj.responseText + ')'); // tout est OK //---------------- alert(objRecuAjax.class_a[0].a); alert(objRecuAjax.b); } //alert("Testing JSON in a javascript intepreter"); try { // b) on crée un objet javascript à partir d'une string JSON issue de variable PHP //---------------------------------------------------------------------------------- var obj = eval('(<?=addslashes($var)?>)'); alert(obj.class_a[0].a); alert(obj.b); // on retransforme cet objet en une string JSON //-------------------------------------------------------------------- var strJSON = encodeURIComponent(JSON.stringify(obj)); // c) on le balance en post via AJAX //------------------------------------- ajax_send(strJSON); } catch (e) { alert("Error:" + e); } </script> <?php echo "<h1>php -> JSON -> javascript -> ajax -> php -> retour ajax -> javascript</h1>"; // print "<pre>".print_r( $json->unserialize( $var ),true)."</pre>"; ?> </body> </html> <?php # ***** end of file test_json.php **** #----------------------------------------- ?> <?php # ***** test_json_ajax.php **** #---------------------------------- require "lib/json.php"; $json = new JSON; $jsonStr = urldecode( $_POST["jsonStr"]); $obj = $json->unserialize( $jsonStr ); $var = $json->serialize( $obj ); // $handle = fopen("d:/tmp/debug2.txt", 'w'); // fwrite($handle, "\n\n---ajax jsonStr ---\n\n"); // fwrite($handle, $jsonStr); // fwrite($handle, "\n\n---ajax ---\n\n"); // fwrite($handle, print_r( $var,true)); // fclose($handle); header("Pragma: no-cache"); header("Expires: 0"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Content-type: text/html; charset=ISO-8859-1"); echo $var; exit; # ***** end of file test_json_ajax.php **** #---------------------------------------------- ?> |
info at phpclasses dot org
.