php json_encode 返回 null

发布于 2024-09-08 05:54:37 字数 1485 浏览 4 评论 0原文

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 加工业务"," 2010-06-21/nordic-capital-buys-sic-processing","PEHub Media","业务","已完成",null, ” 管理员,中国,Frog Capital,德国,意大利,Iyad Omari,制造业,挪威,PEHub Media,光伏晶圆制造,可再生能源,半导体,美国]]}

完成后注意字符串中间的空< /strong>

为什么会这样,我需要执行什么转义/操作才能对其进行编码?

我已经尝试过,addslashes

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 Buys SiC Processing</a></div>","
2010-06-21/nordic-capital-buys-sic-processing</div>","PEHub Media","Business","completed",null,"
Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States]]}

Note the null in the middle of the string after completed

Why is this, what escape/manipulation do I need to perform in order to encode this?

I have tried, addslashes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就是爱搞怪 2024-09-15 05:54:37

从手册:

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

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

From the manual:

Note that if you try to encode an
array containing non-utf values,
you'll get null values in the
resulting JSON string. You can
batch-encode all the elements of an
array with the array_map function:

$encodedArray = array_map(utf8_encode, $myArr);
echo json_encode($encodedArray);
苍风燃霜 2024-09-15 05:54:37

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

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

Actually it doesn't return null, http://codepad.org/A34KdUf5.

Maybe your PHP version doesn't support json_encode().

水水月牙 2024-09-15 05:54:37

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

Works for me on 5.2.13. Ensure you're using at least PHP 5.2.0 and that PHP wasn't compiled with --disable-json. You may also want to check that error reporting (and/or logging) is enabled.

岁月蹉跎了容颜 2024-09-15 05:54:37

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

The simpler way is $store_name = utf8_encode(name_of_varaible) but please make sure that your character set is ISO-8859-1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文