Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by George Petsagourakis )

JSON Helper

Category:Helper -> Community | Category:Helper -> JSON
The JSON helper is a simple interface to the Pear Services_JSON class created by Michal Migurski, Matt Knapp, and Brett Stimmerman.

Only two functions exist, json_encode and json_decode.  Both functions take one parameter, the data to be encoded/decoded.

Once data is encoded, it is decoded in Javascript as so (javascript brackets should be parentheses):

//PHP
$data['encoded_data'] = json_encode($my_data);
$this->load->view('my_view', $data);

//Javascript
eval["var decoded_data = " + <?php echo $encoded_data; ?>];

Save the two attached files to your system/helpers directory.  Load it like any other helper:

$this->load->helper('json');

I think it is better if you put the JSON.php file and the helper to the application/helper directory. Also note, that from PHP 5.2+ upwards the json_encode and json_decode() functions are compiled into core. So a small check would come handy. I have made those changes - download them from here:
File:json helper pengekcs.zip

** The .php file downloads do not work as expected, however, I have left them up for Rick’s examination.  In the mean time, use the .txt downloads but rename them to .php after you have downloaded them.
File:json helper.txt
File:JSON.txt

File:json helper.php
File:JSON.php

Categories: