php json_encode返回null

时间:2010-07-05 15:34:52

标签: php null json

Array
(
    [sEcho] => 1
    [iTotalRecords] => 7521
    [iTotalDisplayRecords] => 1
    [aaData] => Array
        (
            [0] => Array
                (
                    [0] => Nordic Capital Buys SiC Processing
                    [1] => 2010-06-21/nordic-capital-buys-sic-processing
                    [2] => PEHub Media
                    [3] => Business
                    [4] => completed
                    [5] => Nordic Capital has acquired a 70% stake in SiC Processing AG, a German industrial recycling company, from Frog Capital. No sale price was disclosed.  SiC Processing’s founding family retains a 25% holding, while former lead investor Zouk Ventures retains a 5% stake.

                    [6] => Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States
                )

        )

)

echo json_encode($myArr);
  

{ “sEcho”: “1”, “iTotalRecords”:7521, “iTotalDisplayRecords”: “1”, “aaData”:[[”   Nordic Capital购买SiC加工< / a>< / div>“,”   2010-06-21 / nordic-capital-buys-sic-processing< / div>“,”PEHub Media“,”Business“,”completed“, null , “   Admin,China,Frog Capital,德国,意大利,Iyad Omari,制造业,挪威,PEHub媒体,光伏晶圆制造,可再生能源,半导体,美国]]}

请注意完成后字符串中间的空值

为什么这样,我需要执行什么转义/操作来编码呢?

我试过了,addslashes

5 个答案:

答案 0 :(得分:24)

从手册:

  

请注意,如果您尝试编码   包含非utf值的数组,   你将获得空值   生成JSON字符串。您可以   批量编码的所有元素   array_map函数的数组:

$encodedArray = array_map(utf8_encode, $myArr);
echo json_encode($encodedArray);

答案 1 :(得分:1)

我刚刚尝试了完全相同的代码,并返回以下内容:

{"var1":"test string","var2":"Nordic Capital has acquired a 70% stake in SiC Processing AG, a German industrial recycling company, from Frog Capital. No sale price was disclosed.  SiC Processing\u2019s founding family retains a 25% holding, while former lead investor Zouk Ventures retains a 5% stake.","var3":"test string 3"}

您运行的是哪个版本的PHP?这是来自您的本地主机还是您在远程盒子上运行它?

答案 2 :(得分:1)

实际上它不会返回null,http://codepad.org/A34KdUf5

也许您的PHP版本不支持json_encode()。

答案 3 :(得分:1)

在5.2.13上为我工作。确保您至少使用PHP 5.2.0并且PHP未使用--disable-json编译。您可能还想检查是否已启用错误报告(和/或日志记录)。

答案 4 :(得分:0)

更简单的方法是$store_name = utf8_encode(name_of_varaible),但请确保您的字符集为ISO-8859-1.