Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using json_encode in PHP to encode an URL

$json_string = array ('myUrl'=> 'http://example.com');
echo json_encode ($json_string);

The above code generates the following JSON string:

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

Rather than

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

I am just newbie, which output is correct? Is JSON parser able to evaluate the second output correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
488 views
Welcome To Ask or Share your Answers For Others

1 Answer

According to https://www.json.org/, one should escape that character, although it is not strictly necessary in JavaScript:

strings

Also read this related bug report on php.net for a brief discussion.

See 2.5 of the RFC:

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.

So it doesn't sound like it needs to be escaped, but it can be, and the website (and a text diagram in the RFC) illustrates it as being escaped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...