Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Getting the JSON examples to work on PHP4.x

  1. #1
    Sencha Developer
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    482

    Default Getting the JSON examples to work on PHP4.x

    The examples provided by ext using JSON via XHR assume your php to support json_encode().
    For those running php 4.x that might not work out of the box. But it is really easy to fix:

    1.) Download JSON.php from:
    http://mike.teczno.com/JSON/JSON.phps

    2.) copy JSON.php to the example dir of ext.

    3.) Change the php file in the example as follows:
    (here i use the "ext tree" as an example)

    In: example/tree/get-nodes.php add /remark:
    Code:
    require_once("../JSON.php");
    ...
    ...
    ...
    ...
    //echo json_encode($nodes);
    $json = new Services_JSON();
    echo $json->encode($nodes);
    Then it should work.

    Btw.: For those new to JSON here is a good wrap up from ibm of how to turn xml into json:
    http://www-128.ibm.com/developerwork...x-xml2jsonphp/

    Wolfgang

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    6,956

    Default

    Thank you Wolfgang. I am going to sticky this.

  3. #3

    Default

    To make things a little easier you can use the following code:

    Code:
    if(!function_exists('json_encode'))
    {
    	include_once('Services/JSON.php');
    	$GLOBALS['JSON_OBJECT'] = new Services_JSON();
    	function json_encode($value)
    	{
    		return $GLOBALS['JSON_OBJECT']->encode($value); 
    	}
    	
    	function json_decode($value)
    	{
    		return $GLOBALS['JSON_OBJECT']->decode($value); 
    	}
    }
    /*
    You can now use:
    $result = json_encode($your_variable);
    
    $result now contains the json-encoded version of $your_variable.
    */
    For those of you that have access to the apache config, the json module that php5 uses is downloadable here: http://www.aurore.net/projects/php-json/. I really recommend this since it is a lot faster than the php4 implementation.

  4. #4

    Default

    I am totally confused,

    Why and where should I use the given code?
    Code:
     $result = json_encode($nodes);
    and what is $nodes?

    Please help!

  5. #5
    Ext Premium Member BernardChhun's Avatar
    Join Date
    Mar 2007
    Location
    Quebec, Canada
    Posts
    831

    Default

    Quote Originally Posted by noname
    I am totally confused,

    Why and where should I use the given code?
    Code:
     $result = json_encode($nodes);
    and what is $nodes?

    Please help!
    the $nodes variable was only an example. You can use any variable name if you want.

    the json_encode() function turns a PHP associative array (with string, number, boolean values) into a string that will be translated into a javascript object. It's a way of formatting data for communication means just like XML. Read more about it here: http://www.json.org/

  6. #6

    Default

    EDIT: I see you were quicker than me Well, two answers to a question should help you along nicely



    I updated my example to be a bit clearer. The $nodes was just a copy-paste from my own source, you should use your own variable that you want encoded.

    When you use the JsonReader in the grid. The url you supply to the HttpProxy should supply an json-encoded array as a result.

    In the url you supply you have to build up an array with results and encode it with json_encode. You should then echo the result so it will be passed to the jsonReader.

  7. #7
    Ext Premium Member BernardChhun's Avatar
    Join Date
    Mar 2007
    Location
    Quebec, Canada
    Posts
    831

    Default

    Quote Originally Posted by mnugter
    EDIT: I see you were quicker than me Well, two answers to a question should help you along nicely :
    there was actually 2 questions and I answered to only one part of the Why one.

    Then When's answer is anytime PHP has to communicate with your web app!

  8. #8
    Sencha User
    Join Date
    Mar 2007
    Posts
    43

    Default also error reporting

    Hopefully this might additionally save someone some time - if you browse to /ext/examples/tree/get-nodes.php in your browser you might see various warnings and notices about undefined indexes and such. These can all be ignored, however the output goofs up the JSON output and so the treeview and other examples don't work.

    In addition to the JSON code mentioned above, you can tell PHP to not output all the notices by adding this line somewhere at the top of the script:

    error_reporting(E_ERROR);

  9. #9
    Sencha User
    Join Date
    Mar 2007
    Posts
    464

    Default

    Zend Framework also has support for serializing JSON data.

    http://framework.zend.com/manual/en/zend.json.html

  10. #10

    Default Thanks Wolfgang

    Thanks Wolfgang - the penny finally dropped when I read your post - all makes perfect sense now..

Page 1 of 3 123 LastLast

Similar Threads

  1. some examples.....
    By genius551v in forum Community Discussion
    Replies: 8
    Last Post: 25 Mar 2008, 10:40 AM
  2. examples about PropertyGrid?
    By franklt69 in forum Ext 2.x: Help & Discussion
    Replies: 8
    Last Post: 23 Nov 2007, 5:45 AM
  3. TreePanel examples
    By dolittle in forum Ext 2.x: Help & Discussion
    Replies: 5
    Last Post: 19 Mar 2007, 2:13 AM
  4. Examples (ala YUI)
    By allaboutyui in forum Ext 1.x: Help & Discussion
    Replies: 6
    Last Post: 14 Nov 2006, 3:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •