【发布时间】:2016-01-14 14:50:03
【问题描述】:
json_decode('["foo","bar"]', true),这可行,但返回NULL,json_decode("['foo','bar']", true)。 json_last_error() 输出 4、JSON_ERROR_SYNTAX。
我检查了以下问题的一些答案;
json_decode() returns null issues
PHP json_decode() returns NULL with valid JSON?
json_decode returns NULL after webservice call
并尝试了以下解决方案但没有成功;
json_decode(str_replace('"', '"', "['foo','bar']"), true)
json_decode(stripslashes(str_replace('\"', '"', "['foo','bar']")), true)
json_decode(stripslashes("['foo','bar']"), true)
json_decode(utf8_encode("['foo','bar']"), true)
我认为这与 UTF-8 bom 无关。它是一个PHP错误吗?或者我如何将"['foo','bar']" 变成'["foo","bar"]' 作为解决方法?
【问题讨论】:
-
函数
json_decode作为参数的字符串需要是有效的JSON。{"a":1,"b":2,"c":3,"d":4,"e":5}这是有效的 JSON。你只是传入一个用引号括起来的数组。