How to use json_encode without PHP 5.2

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:alt text

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?

+3
4

json_encode . .

PECL, , @Artefacto.

+4

You can use the PECL extension .

+1
source

You can use json_encodefor earlier versions of PHP 5.x if you are using an older version of PHP. It works great!

0
source

Source: https://habr.com/ru/post/1749382/


All Articles