Part of the EllisLab Network
   
 
JSON helper
Posted: 28 August 2006 08:31 AM   [ Ignore ]  
Summer Student
Total Posts:  2
Joined  08-28-2006

A small request for anyone who has a free minute or 2 at hand. Would be nice to see a JSON helper for CI. Maybe based on http://mike.teczno.com/json.html ??

Profile
 
 
Posted: 28 August 2006 12:25 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

JSON Helper

Threw it together in like two seconds, untested, let me know if it works

Profile
 
 
Posted: 30 August 2006 05:17 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  2
Joined  08-28-2006

cheers - will take a look at it and let you know smile

Profile
 
 
Posted: 30 August 2006 08:44 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  17
Joined  04-15-2006

It should be included in next CI version.

Profile
 
 
Posted: 01 September 2006 08:54 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  201
Joined  08-28-2006

I tried to download the files from the WIKI and got the following error message:
“No direct script access allowed”

Any ideas ?
Thanks alot…shocki

Profile
 
 
Posted: 02 September 2006 07:03 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  6
Joined  08-16-2006
shocki - 01 September 2006 08:54 AM

I tried to download the files from the WIKI and got the following error message:
“No direct script access allowed”

Any ideas ?
Thanks alot…shocki

I think there is a problem with the .php extension, but you can access the file in http://www.codeigniter.com/images/wiki_uploads/json_helper.txt.

Profile
 
 
Posted: 02 September 2006 12:27 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

It’s odd that the wiki would allow me to rename my files to .php and then execute them when somebody would try to download them.  In this way, the Wiki appears to be very insecure.  In the meantime, I have replaced the .php files with .txt files.

Profile
 
 
Posted: 02 September 2006 01:26 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  17
Joined  04-15-2006

Heh, it is a critical bug wink

BTW., I changed my mind. It should NOT be included in CI, because it depends on PEAR. If it will be independent, then - it should be included wink

Profile
 
 
Posted: 02 September 2006 03:23 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

Personally, I never really thought it should be included, given that it is never going to be used by probably more than two thirds of CI users.  Plus, the two “helper” functions are really just middle men to the Services_JSON methods.  You could easily use that class without the helper, or write your own.  However, it is only as dependent on PEAR as any other third party code is dependent on outside sources.  It does not require a PEAR installation or any other PEAR classes.  If you were to argue this, you’d also have to argue that any code that uses an outside source should not be added to the CI core.

Profile
 
 
Posted: 07 September 2006 04:17 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  1
Joined  09-07-2006

*** Update ***

I think I had a brain fart at the time when I was trying to get this to work. I hadn’t created the Services_JSON object.

Here’s the updated and working function found in my controller:

function ajax(){
        log_message
("debug","starting ajax");

        
$this->load->helper('json');
        
$json = new Services_JSON();
        
        
$msg = array("test"=>"a simple test");
        
$jobj = $json->encode($msg);
        
log_message("debug","json output: ".$jobj);
        echo
$jobj;
    
}

————Old Message Below——————-
I’m getting the following error when trying to use the JSON Helper from the wiki (http://www.codeigniter.com/wiki/JSON_Helper/).

Fatal error: Call to a member function on a non-object in C:\Apache\...\system\helpers\json_helper.php on line 49

Line 49 represents the following in the json_helper.php file:

return $json->encode($data);

I’ve also tried using what I’ve found from another helper for object management through CI (found below), but that didn’t work either. I’m not sure if what I tried is even possible, but thought it worth a try.

$obj =& get_instance();
$obj->json= new Services_JSON();
$obj->ci_is_loaded[] = 'json';

Here’s the function call that starts the whole thing:

function ajax(){
    
// used to get dropdown data
    
log_message("debug","starting ajax");
    
$this->load->helper('json');
    
$msg = array("test"=>"a simple test");
    
$jobj = json_encode($msg);
        
log_message("debug","json output: ".$jobj);
}

Any info on fixing my problem would be greatly appreciated.

Profile
 
 
Posted: 02 December 2006 01:56 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  95
Joined  10-24-2006

Here’s what I did. Mind you, php 5.2.0 has built in support for the json extension now. However, for those of us developing on a shared host with an older version of php you can do this.

First, save this as a library with the filename Services_JSON.php - http://mike.teczno.com/JSON/JSON.phps

Then load it as a libray like this

$this->load->library('services_json');

Then you can use the functions like decode and encode. Here’s an example.

$output['json'] = $this->services_json->encode($data);


Note If you have access to the php json extension, use it. It’s much quicker. This is mainly a tool for those of us who don’t have current access to it. If/when you move to having access to the json extension, you’ll want to refactor your code to use it.

Note 2 There’s a Pear::UNIT class or something that the class I posted relies on for debugging purposes. It is NOT a requirement for using this class in it’s default state.

Profile
 
 
Posted: 19 February 2008 05:42 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  14
Joined  09-21-2007

I only get a white screen when attempting this—I also attempted this as a helper and I only get a white screen everytime. Anyone have any ideas? Maybe constant collisions from the JSON Pear class?

Thanks

Profile
 
 
   
 
 
‹‹ Feed Library request      deep_in_array () ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77540 Total Logged-in Users: 33
Total Topics: 101541 Total Anonymous Users: 3
Total Replies: 544326 Total Guests: 255
Total Posts: 645867    
Members ( View Memberlist )