Welcome Guest, Not a member yet? Register   Sign In
json encode and decode for < php 5.2
#1

[eluser]Fabdrol[/eluser]
Since php 5.2 you can encode mixed variables into JSON strings, like this:

Code:
$arr = array('item 1' => 'value 1', 'item 2' => 'value 2', 'item 3' => 'value 3');
$json = json_encode($arr); // returnst: { "item 2":"value 2", "item 3":"value 3", "item 3":"value 3"  }

Unfortunately, that doesn't work on earlier versions of php. A simple model will solve this problem, although it is really simple, could be brushed up with object and array difference, file output, etc..

but, for all of you who seek something like this; here it is.

Code:
&lt;?php

    class Json extends Model {
    
        function Json() {
            parent::Model();
        }
        
        function encode($arr) {            
            $str = '{';
            foreach($arr as $key => $value) {
                $str .= '"'.$key.'":"'.$value.'",';
            }
            
            $out = substr_replace($str, "", -1);
            $out .= '}';
            
            return $out;
        }
        
        function decode($str) {
            $arr = array('index' => array(), 'waarde' => array());
            $out = array();
        
            $str = str_replace('{', '', $str);
            $str = str_replace('}', '', $str);
            $str = str_replace('"', '', $str);
            
            
            $dirty = explode(',', $str);
            
            foreach($dirty as $set) {
                $pair = explode(':', $set);
                $out[$pair[0]] = $pair[1];
            }
            
            return $out;
        }
    
    }

Fabian.
#2

[eluser]Jamie Rumbelow[/eluser]
Thanks, but this is nowhere near robust enough for any decent usage. You can't have objects inside of arrays, arrays inside of objects, arrays inside of arrays etc. etc.
#3

[eluser]Fabdrol[/eluser]
@Jamie,

True, true true. But that isn't needed, I use it as a simplified way to store array's into database columns.
the class on this site is more helpful if you want real json encode and decode functions.. http://blueberryware.net/2008/10/28/php4...ode-decode

anyway, thnx for your reply
#4

[eluser]Flipside Tech[/eluser]
I have made a helper for JSON.

1. Download: http://flipsideservices.com/jsonwrapper.zip (or see attached)
2. Extract into the application helper directory
3. Edit autoload.php and add 'jsonwrapper/jsonwrapper' to the helper array.
4. Done.... crack open a beer :-)

In the case that you have PHP >= 5.2, this code will be ignored.

Enjoy.

Ryan
#5

[eluser]Colin Williams[/eluser]
Just using PHP 5.2 seems so much easier...
#6

[eluser]Flipside Tech[/eluser]
Indeed, but where PHP 5.2 is not an option, this helper takes 30 seconds to implement.
#7

[eluser]Fabdrol[/eluser]
Ey Flipside,

nice helper, but I think I skip steps 1,2,3 and proceed on crackin' open that bottle of beer!
In my application I don't need fancy json, just some really simple string to array and back conversion!
but still, I appreciate your input.
#8

[eluser]Erk[/eluser]
Flipside, you just saved me, I just implemented this and it took no time at all!

I know I should update my client's PHP but I had to install a major update tonight and this is the only JSON_Encode replacement that did the trick!

Thanks again
#9

[eluser]Unknown[/eluser]
If the other links aren't working you can visit this site to find some good information about using <a href="http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/">PHP4 Json Encode &amp; Decode</a>
#10

[eluser]rckehoe[/eluser]
[quote author="Flipside Tech" date="1228816747"]I have made a helper for JSON.

1. Download: http://flipsideservices.com/jsonwrapper.zip (or see attached)
2. Extract into the application helper directory
3. Edit autoload.php and add 'jsonwrapper/jsonwrapper' to the helper array.
4. Done.... crack open a beer :-)

In the case that you have PHP >= 5.2, this code will be ignored.

Enjoy.

Ryan[/quote]

Just wanted to say, you rock! This helped me out quite a bit!




Theme © iAndrew 2016 - Forum software by © MyBB