2010-06-10 53 views
3

我已经写了一个CMS,它使用PHP函数json_encode通过Ajax请求发送一些数据。如何使用json_encode不使用PHP 5.2

不幸的是,我试图将它加载到运行PHP 5.1版本的服务器上,json_encode PHP函数在5.2.0之前版本的PHP上不可用。

有没有人知道一种方法来编码一个PH数组作为JSON,而不使用内置的json_encode函数?

编辑

我用佩卡的功能,但现在我发现按预期jQuery将无法解析的结果。即使Firebug显示JSON被传回。我的萤火虫窗口如下所示:alt text http://www.freeimagehosting.net/uploads/b5a5e5855b.jpg

和我的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']); 

不过的div:#DIV1和#DIV2不会加载正确的数据。

为什么?

+0

我假设你已经正确关闭了ajax声明?关闭成功功能,然后整体ajax功能? – 2011-03-15 16:31:40

+1

是的,他们被正确关闭,我只是包括相关部分的代码 – Ash 2011-03-16 10:06:49

回答

4

json_encode的用户注释有很多实现。匆匆一瞥,this one看起来最适合我。

如果您有权访问PECL,我会使用@Artefacto建议的扩展名。

+0

好吧,我正在测试它... – Ash 2010-06-10 11:40:45

+0

确定,工作正常 - 萤火虫显示JSON通过。然而,jquery不喜欢它... – Ash 2010-06-10 12:03:42

+0

@Ashley也许显示一些示例JSON ... – 2010-06-10 12:05:24