加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:37:18 所属栏目:PHP教程 来源:网络整理
导读:我在 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"} 我只是新手,哪
我在 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中的一个文本图)说明它被转义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读