PHP的json_decode出现单引号而不是双引号的问题 [英] PHP's json_decode having problems with single but not double quotes

查看:246
本文介绍了PHP的json_decode出现单引号而不是双引号的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过示例最容易解释:

var_dump(json_decode("[{'a':'b'},{'c':'d'},{'e':'f'}]")); // NULL
var_dump(json_decode('[{"a":"b"},{"c":"d"},{"e":"f"}]')); // array(3) { [0]=> object(stdClass)#1 (1) { ["a"]=> string(1) "b" } [1]=> object(stdClass)#2 (1) { ["c"]=> string(1) "d" } [2]=> object(stdClass)#3 (1) { ["e"]=> string(1) "f" } }

如您所见,第一个使用单引号的示例将返回NULL,这意味着出现错误,而第二个示例可以正常工作.

As you can see the first example where single quotes are used returns NULL meaning an error while the second one works fine.

除了为什么要进行一堆字符串操作外,是否有其他想法,或者我可以采取哪些措施来防止出现问题?

Any idea as to why it is doing it or what I can do to help prevent problems other than doing a bunch of string manipulation?

推荐答案

JSON中的字符串定义为:

A string in JSON is defined as:

""
" chars "

换句话说,JSON字符串需要双引号(不是单引号).您如何获得此JSON?我们可以看看解码之前验证/修复字符串的可能解决方案.

In other words, double quotes (not single) are necessary for JSON strings. How are you getting this JSON? We can look at a possible solution for validating/fixing the string before decoding.

来源: http://www.json.org/

值可以是双引号中的字符串,也可以是[...]

A value can be a string in double quotes, or [...]

这篇关于PHP的json_decode出现单引号而不是双引号的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆