简体   繁体   English

如何在没有PHP 5.2的情况下使用json_encode

[英]how to use json_encode without PHP 5.2

I've written a CMS which uses the PHP function json_encode to send some data back via an Ajax Request. 我编写了一个CMS,它使用PHP函数json_encode通过Ajax请求发回一些数据。

Unfortunately, I'm trying to load it onto a server which is running PHP version 5.1, the json_encode PHP function is not available on versions of PHP before 5.2.0. 不幸的是,我正在尝试将其加载到运行PHP版本5.1的服务器上,json_encode PHP函数在5.2.0之前的PHP版本上不可用。

Does anyone know of a way to encode a PH array as JSON without using the inbuilt json_encode function? 有没有人知道如何在不使用内置的json_encode函数的情况下将PH数组编码为JSON?

EDIT 编辑

I've used Pekka's function, but now I find JQuery won't parse the result as expected. 我已经使用了Pekka的函数,但现在我发现JQuery不会按预期解析结果。 Even though Firebug shows JSON being passed back through. 即使Firebug显示JSON被传回。 My firebug window looks like this: 我的firebug窗口看起来像这样: 替代文字

and my jquery looks like this: 我的jquery看起来像这样:

     $.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 the divs : #div1 and #div2 will not load the correct data. 但div:#div1和#div2不会加载正确的数据。

FOR WHY? 为什么?

The User Contributed Notes to json_encode have a number of implementations. 用户对json_encode的贡献说明有许多实现。 On a cursory glance, this one looks the best to me. 粗略地看一眼, 这个看起来对我来说是最好的。

If you have access to PECL, I would use the extension as @Artefacto recommends. 如果您有权访问PECL,我会使用@Artefacto推荐的扩展名。

I hit up the google... This person has made a encoder of their own: 我打了谷歌......这个人制作了自己的编码器:

http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/ http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/

您可以使用PECL扩展

You can use json_encode for earlier versions of PHP 5.x if you are using an older version of PHP. 如果您使用的是旧版本的PHP,则可以将json_encode用于早期版本的PHP 5.x. It works great! 它很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM