[英]json_decode return null with different qoute
json_decode('["foo","bar"]', true)
, this works, but this return NULL
, json_decode("['foo','bar']", true)
. json_decode('["foo","bar"]', true)
,这json_decode("['foo','bar']", true)
,但是返回NULL
, json_decode("['foo','bar']", true)
。 The json_last_error()
outputs 4
, JSON_ERROR_SYNTAX
. json_last_error()
输出4
, JSON_ERROR_SYNTAX
。
I've checked some answers from following questions; 我已经检查了以下问题的一些答案;
json_decode() returns null issues json_decode()返回空问题
PHP json_decode() returns NULL with valid JSON? PHP json_decode()使用有效JSON返回NULL?
json_decode returns NULL after webservice call 网络服务调用后json_decode返回NULL
and tried following solutions but no success; 并尝试了以下解决方案,但没有成功;
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)
I don't think it has to do with UTF-8 bom. 我认为这与UTF-8 Bom没有关系。 Is it a PHP bug? 它是PHP错误吗? Or how do I do turn "['foo','bar']"
into '["foo","bar"]'
as a workaround? 或如何将"['foo','bar']"
变成'["foo","bar"]'
作为解决方法?
You are not providing valid json to a function. 您没有为函数提供有效的json。 If u using javascript array 如果你使用JavaScript数组
["foo","bar"]You should use JSON.stringify to make JSON actually on js side. 您应该使用JSON.stringify在JS端实际制作JSON。
JSON strings are quoted with double quotes "
. Single quotes '
(which are common in PHP) are not valid JSON. No discussion. Thus, the input ['foo','bar']
is not valid json and json_decode
correctly refuses to parse it . JSON字符串用双引号"
。单引号'
(在PHP中很常见)不是有效的JSON。没有讨论。因此,输入['foo','bar']
是无效的json,并且json_decode
正确拒绝解析它 。
Also see ECMA-404 , which defines the JSON format: 另请参阅ECMA-404 ,它定义了JSON格式:
A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). 字符串是用引号(U + 0022)包裹的Unicode代码点的序列。 1 1个
If you're looking for something to transform your JSON-ish string (where does it come from? Fix the source of the invalid JSON, preferably) into valid JSON; 如果您正在寻找将JSON式字符串(它来自哪里?最好将无效JSON的源修复)转换为有效JSON的方法; str_replace('\\'', '"', $jsonInput)
should work in simple cases. str_replace('\\'', '"', $jsonInput)
在简单情况下应该可以工作。
1 ( U+0022
is the double quote "
) 1 ( U+0022
是双引号"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.