繁体   English   中英

json_decode使用不同的qoute返回null

[英]json_decode return null with different qoute

json_decode('["foo","bar"]', true) ,这json_decode("['foo','bar']", true) ,但是返回NULLjson_decode("['foo','bar']", true) json_last_error()输出4JSON_ERROR_SYNTAX

我已经检查了以下问题的一些答案;

json_decode()返回空问题

PHP json_decode()使用有效JSON返回NULL?

网络服务调用后json_decode返回NULL

并尝试了以下解决方案,但没有成功;

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。 如果你使用JavaScript数组

["foo","bar"]
您应该使用JSON.stringify在JS端实际制作JSON。

JSON字符串用双引号" 。单引号' (在PHP中很常见)不是有效的JSON。没有讨论。因此,输入['foo','bar'] 是无效的json,并且json_decode正确拒绝解析它

另请参阅ECMA-404 ,它定义了JSON格式:

字符串是用引号(U + 0022)包裹的Unicode代码点的序列。 1个

如果您正在寻找将JSON式字符串(它来自哪里?最好将无效JSON的源修复)转换为有效JSON的方法; str_replace('\\'', '"', $jsonInput)在简单情况下应该可以工作。


1U+0022是双引号"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM