PHP json_decode()表现得很奇怪

时间:2009-01-21 20:22:16

标签: php json

我有两个类似的JSON文件。我可以用json_decode()读取其中一个,但不能读到另一个。

我已将文件上传到www.huzursuz.com/json/json_test.rar

如果您想查看它们,brother_a.php正在运行,而brother.php则没有。

我认为问题不是json_decode嵌套限制,因为文件非常相似。

1 个答案:

答案 0 :(得分:4)

你从哪里获得那个巨大的JSON字符串?

根据json_decode文档,如果json在某些方面格式不正确,它将只返回NULL,这是我在尝试使用brother.php时所得到的

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null

编辑

我通过JSONLint(一个JSON验证程序)运行了两个JSON文件,并且正如预期的那样,传递了brother+a文件,而brother在几个位置上格式不正确。

相关问题