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

前端之家收集整理的这篇文章主要介绍了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中的一个文本图)说明它被转义.

原文链接:https://www.f2er.com/php/131783.html

猜你在找的PHP相关文章