微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 为什么JSON编码器在编码URL时会添加转义字符?

我在 PHP中使用json_encode来编码一个URL
$json_string = array ('myUrl'=> 'http://example.com');
echo json_encode ($json_string);

上述代码生成以下JSON字符串:

{"myUrl":"http:\/\/example.com"}

而不是

{"myUrl":"http://example.com"}

我只是新手,哪个输出是正确的? JSON解析器能否正确评估第二个输出

根据 http://www.json.org/,一个人应该逃避这个角色,虽然JavaScript并不是绝对必要的:

还可以在PHP.net上阅读这个related bug report,进行简短的讨论.

参见RFC中的2.5:

All Unicode characters may be placed
within the quotation marks except for
the characters that must be escaped:
quotation mark,reverse solidus,and
the control characters (U+0000 through
U+001F).

Any character may be escaped.

所以它听起来不像是需要被转义,但它可以,网站(和RFC中的一个文本图)说明它被转义.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐