PHP:json_decode转储NULL,找不到BOM

时间:2011-01-14 20:30:32

标签: php json

我一直在试图找出为什么这个'json_encode'd字符串没有正确解析出来,并且遇到了之前回答的问题,这些问题的UTF BOM序列引发了错误,但在这里没有帮助我

以下是目前无效的代码:

//Decode the notes attached to the sig
$aNotes = json_decode($rule->getNotes(),true);
$bom = pack("CCC",0xef,0xbb,0xbf);
if(0 == strncmp($rule->getNotes(),$bom,3))
{
    print('BOM detected - json encoding in UTF-8<br/>');
}
else
{
    print('BOM NOT detected - json encoding correctly<br/>');
}
print('rule->getNotes:<br/>' . $rule->getNotes() .'<br/>');
var_dump($aNotes);

生成此结果:

BOM NOT detected - json encoding correctly
rule->getNotes:
[{"lDate":"Unknown","sAuthor":"Unknown","sNote":"This is a general purpose Russian spam rule that matches anything starting with 2, 3 or 4 hex digits followed by a domain name ending with .ru -RSK 2010-05-10"},{"lDate":"1295031463082","sAuthor":"Drew Thorstenson","sNote":"this is Ryan's ru rule"}]
NULL

我通过JSON Lint运行它,它表示它是有效的,并且在线JSON Parser也正确地解析了它。

非常感谢任何见解。

1 个答案:

答案 0 :(得分:1)

问题不在于BOM。也许,您在将json字符串存储在数据库中之前对json字符串执行了htmlspecialchars,因此它具有引号而不是引号的HTML实体。为了使它工作,你可以使用:

$aNotes = json_decode(htmlspecialchars_decode($rule->getNotes()),true);

或者只是在存储JSON数据时不执行htmlspecialchars