php json_encode 返回false
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php json_encode 返回false相关的知识,希望对你有一定的参考价值。
参考技术A json_encode() 所有字符串数据的编码必须是 UTF-8,不然返回false.json_encode() 返回 false
【中文标题】json_encode() 返回 false【英文标题】:json_encode() returns false 【发布时间】:2013-10-26 18:11:46 【问题描述】:这是我第一次面对,var_dumping json_encode 的数组结果布尔值。
我有一个由反序列化产生的数组。我 var_dumped 它并确保它是一个有效的数组。结果如下所示。这只是一个片段,而不是整个调试视图。
array (size=3)
'id' => string 'asco-power-technologies-l-p' (length=27)
'name' => string 'asco-power-technologies-l-p' (length=27)
'children' =>
array (size=2)
0 =>
array (size=4)
'id' => string 'apl-logistics' (length=13)
'name' => string 'APL LOGISTICS' (length=13)
'data' =>
array (size=2)
'band' => string 'ASCO POWER TECHNOLOGIES ,L.P.' (length=29)
'relation' => string 'Buyer of band' (length=13)
'children' =>
array (size=11)
0 => &
array (size=3)
'id' => string 'trisa-ag' (length=8)
'name' => string 'TRISA AG' (length=8)
'data' =>
array (size=2)
'band' => string 'APL LOGISTICS' (length=13)
'relation' => string 'Buyer of band' (length=13)
1 => &
array (size=3)
'id' => string 'colgate-sanxiao-co-ltd' (length=22)
'name' => string 'COLGATE SANXIAO CO LTD' (length=22)
'data' =>
array (size=2)
'band' => string 'APL LOGISTICS' (length=13)
'relation' => string 'Buyer of band' (length=13)
.....
Aterwards 我用最大深度=3 对其进行 json_encode。请注意,我使用的是 PHP 5.5 并在 Apache 2.4 上运行,因此它支持 json_encode() 上的深度参数。这就是我正在做的。
$array = unserialize($serial);
var_dump($array);
$json = json_encode($array, JSON_PRETTY_PRINT, 3);
var_dump($json);
最后我得到了正确的$array
var_dumped 和$json
var_dumped 的错误布尔值结果。它出什么问题了?是否由于早期的 PHP 版本,不正确的 var_dump 会导致错误的布尔值?有人遇到过这种情况吗?
更新:
这是序列化字符串。整个字符串太束了,所以我只粘贴了一段。
a:3:s:2:"id";s:27:"asco-power-technologies-l-p";s:4:"name";s:27:"asco-power-technologies-l-p";s:8:"children";a:2:i:0;a:4:s:2:"id";s:13:"apl-logistics";s:4:"name";s:13:"APL LOGISTICS";s:4:"data";a:2:s:4:"band";s:29:"ASCO POWER TECHNOLOGIES ,L.P.";s:8:"relation";s:13:"Buyer of band";s:8:"children";a:11:i:0;a:3:s:2:"id";s:8:"trisa-ag";s:4:"name";s:8:"TRISA AG";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:1;a:3:s:2:"id";s:22:"colgate-sanxiao-co-ltd";s:4:"name";s:22:"COLGATE SANXIAO CO LTD";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:2;a:3:s:2:"id";s:25:"apll-on-behalf-oftongfang";s:4:"name";s:25:"APLL ON BEHALF OFTONGFANG";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:3;a:3:s:2:"id";s:26:"apll-on-behalf-of-zhejiang";s:4:"name";s:26:"APLL ON BEHALF OF ZHEJIANG";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:4;a:3:s:2:"id";s:33:"apll-on-behalf-of-tongfang-global";s:4:"name";s:33:"APLL ON BEHALF OF TONGFANG GLOBAL";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:5;a:3:s:2:"id";s:34:"apll-on-behalf-of-ningbo-xiangyuan";s:4:"name";s:34:"APLL ON BEHALF OF NINGBO XIANGYUAN";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:6;a:3:s:2:"id";s:33:"apll-on-behalf-of-ningbo-homestar";s:4:"name";s:33:"APLL ON BEHALF OF NINGBO HOMESTAR";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:7;a:3:s:2:"id";s:17:"apll-on-behalf-of";s:4:"name";s:17:"APLL ON BEHALF OF";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:8;a:3:s:2:"id";s:21:"apl-logistics-sdn-bhd";s:4:"name";s:21:"APL LOGISTICS SDN BHD";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";i:9;a:4:s:2:"id";s:21:"apl-logistics-limited";s:4:"name";s:21:"APL LOGISTICS LIMITED";s:4:"data";a:2:s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";s:8:"children";a:27:i:0;a:3:s:2:"id";s:26:"tri-phil-international-inc";s:4:"name";s:27:"TRI-PHIL INTERNATIONAL INC.";s:4:"data";a:2:s:4:"band";s:21:"APL LOGISTICS LIMITED";s:8:"relation";s:13:"Buyer of band";i:1;a:3:s:2:"id";s:16:"pt-apl-logistics";s:4:"name";s:17:"PT. APL LOGISTICS";s:4:"data";a:2:s:4:"band";s:21:"APL LOGISTICS LIMITED";s:8:"relation";s:13:"Buyer of band";i:2;a:3:s:2:"id";s:25:"keygold-manufacturing-inc";s:4:"name";s:26:"KEYGOLD ...
【问题讨论】:
谢谢!你的问题帮助了我!就我而言,json_encode 由于一个变量的编码而返回 false ...我需要在执行 json_encode 之前对变量执行 utf8_encode ...现在它可以工作了!问候 【参考方案1】:$array
的深度肯定大于 3。只要将深度设为更大的数字即可。
附带说明,您也可以使用json_last_error
在json_encode 失败的情况下查看错误消息。
【讨论】:
我只是将深度更改为 4、5、10 甚至 100 并且仍然得到相同的结果。 如果您使用 PHP 页面中的示例 #1,那么很可能是 JSON_ERROR_RECURSION、JSON_ERROR_INF_OR_NAN 或 JSON_ERROR_UNSUPPORTED_TYPE,因为如果您没有修改处理它们的代码,那么它们将显示为“未知错误”。 从 5.5.0 开始 json_last_error_msg 会给你一个实际的错误信息而不是错误代码。【参考方案2】:试试这个功能,因为它已经在 php.net 提出了
function array_to_json( $array )
if( !is_array( $array ) )
return false;
$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
if( $associative )
$construct = array();
foreach( $array as $key => $value )
// We first copy each key/value pair into a staging array,
// formatting each key and value properly as we go.
// Format the key:
if( is_numeric($key) )
$key = "key_$key";
$key = '"'.addslashes($key).'"';
// Format the value:
if( is_array( $value ))
$value = array_to_json( $value );
else if( !is_numeric( $value ) || is_string( $value ) )
$value = '"'.addslashes($value).'"';
// Add to staging array:
$construct[] = "$key: $value";
// Then we collapse the staging array into the JSON form:
$result = " " . implode( ", ", $construct ) . " ";
else // If the array is a vector (not associative):
$construct = array();
foreach( $array as $value )
// Format the value:
if( is_array( $value ))
$value = array_to_json( $value );
else if( !is_numeric( $value ) || is_string( $value ) )
$value = '"'.addslashes($value).'"';
// Add to staging array:
$construct[] = $value;
// Then we collapse the staging array into the JSON form:
$result = "[ " . implode( ", ", $construct ) . " ]";
return $result;
【讨论】:
以上是关于php json_encode 返回false的主要内容,如果未能解决你的问题,请参考以下文章