I wrote a CMS that uses the json_encode PHP function to send some data back using an Ajax request.
Unfortunately, I am trying to upload it to a server running PHP version 5.1, the PHP function json_encode is not available in versions of PHP prior to 5.2.0.
Does anyone know a way to encode a PH array as JSON without using the built-in json_encode function?
EDIT
I used the Pekka function, but now I found that jQuery will not parse the result as expected. Despite the fact that Firebug shows that JSON is passed back. My firebug window looks like this:
and my jQuery looks like this:
$.ajax({
type: "GET",
url: "includes/function/add_users.php",
data: str,
dataType: 'json',
cache: false,
beforeSend: function(html){
$('#editbox').html('<img class="preloader" src="images/ajax-loader.gif"/>');
},
success: function(html){
fields = html;
$('#div1').html(fields['username']);
$('#div2').html(fields['fname']);
But divs: # div1 and # div2 will not load the correct data.
WHY?