If I use json_encode() on an array like this:
如果我在这样的数组上使用json_encode():
return json_encode(array( 'foo' => 'bar'));
The return is:
回报是:
{'foo' : 'bar'}
The key is passed as a literal, and that is tripping up my script. What I really need is:
密钥以文字形式传递,这使我的脚本绊倒。我真正需要的是:
{ foo : 'bar' }
Does json_encode do that or do I have to strip the quotes out myself with some ugly regex?
json_encode是否会这样做,还是我必须用一些丑陋的正则表达式来删除引号?
4 个解决方案
#1
14
When I test this portion of code :
当我测试这部分代码时:
echo json_encode(array( 'foo' => 'bar'));
die;
I get :
我明白了:
{"foo":"bar"}
Which is valid JSON.
哪个是有效的JSON。
(Note these are double-quotes, and not simple-quotes as you posted)
(注意这些是双引号,而不是你发布的简单引号)
The ouput you are asking for :
你要求的输出:
{ foo : 'bar' }
is valid Javascript, but is not valid JSON -- so json_encode
will not return that.
是有效的Javascript,但是无效的JSON - 所以json_encode不会返回它。
See json.org for the specification of the JSON format -- which is a subset of Javascript, and not Javascript itself.
有关JSON格式的规范,请参阅json.org - 这是Javascript的一个子集,而不是Javascript本身。
Instead of "stripping the quotes out myself with some ugly regex", you should adapt your code, so it accepts valid JSON : this is way better, in my opinion.
你应该调整你的代码,而不是“用一些丑陋的正则表达式来剥离引号”,所以它接受有效的JSON:在我看来,这样做更好。
#2
2
No, json_encode
will not do this for you. The json specification specifcally requires that keys be quoted strings. This is done to ensure that keys which are javascript reserved words won't break the data object.
不,json_encode不会为你做这件事。 json规范特别要求键是带引号的字符串。这样做是为了确保作为javascript保留字的密钥不会破坏数据对象。
#3
0
How is it tripping up your script?
它是如何绊倒你的剧本的?
And per the JSON specification, key names are supposed to be strings. The 2nd snippet you posted is not valid JSON.
根据JSON规范,键名应该是字符串。您发布的第二个片段不是有效的JSON。
#4
0
Thanks, everyone. I did not know that about the JSON spec. The issue was in fact with my script because I had not set the datatype of my $.ajax() function to "json"
感谢大家。关于JSON规范,我不知道。问题实际上是我的脚本,因为我没有将$ .ajax()函数的数据类型设置为“json”
What I learned today -- JSON and Javascript are not the same thing!
我今天学到的东西--JSON和Javascript不是一回事!
#1
14
When I test this portion of code :
当我测试这部分代码时:
echo json_encode(array( 'foo' => 'bar'));
die;
I get :
我明白了:
{"foo":"bar"}
Which is valid JSON.
哪个是有效的JSON。
(Note these are double-quotes, and not simple-quotes as you posted)
(注意这些是双引号,而不是你发布的简单引号)
The ouput you are asking for :
你要求的输出:
{ foo : 'bar' }
is valid Javascript, but is not valid JSON -- so json_encode
will not return that.
是有效的Javascript,但是无效的JSON - 所以json_encode不会返回它。
See json.org for the specification of the JSON format -- which is a subset of Javascript, and not Javascript itself.
有关JSON格式的规范,请参阅json.org - 这是Javascript的一个子集,而不是Javascript本身。
Instead of "stripping the quotes out myself with some ugly regex", you should adapt your code, so it accepts valid JSON : this is way better, in my opinion.
你应该调整你的代码,而不是“用一些丑陋的正则表达式来剥离引号”,所以它接受有效的JSON:在我看来,这样做更好。
#2
2
No, json_encode
will not do this for you. The json specification specifcally requires that keys be quoted strings. This is done to ensure that keys which are javascript reserved words won't break the data object.
不,json_encode不会为你做这件事。 json规范特别要求键是带引号的字符串。这样做是为了确保作为javascript保留字的密钥不会破坏数据对象。
#3
0
How is it tripping up your script?
它是如何绊倒你的剧本的?
And per the JSON specification, key names are supposed to be strings. The 2nd snippet you posted is not valid JSON.
根据JSON规范,键名应该是字符串。您发布的第二个片段不是有效的JSON。
#4
0
Thanks, everyone. I did not know that about the JSON spec. The issue was in fact with my script because I had not set the datatype of my $.ajax() function to "json"
感谢大家。关于JSON规范,我不知道。问题实际上是我的脚本,因为我没有将$ .ajax()函数的数据类型设置为“json”
What I learned today -- JSON and Javascript are not the same thing!
我今天学到的东西--JSON和Javascript不是一回事!