Why does JSON encoder adds escaping character when encoding URLs?

Viewed 16215

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?

5 Answers

I see another problem here. The string result {"myUrl":"http://example.com"} should not have the member name myUrl quoted. In JavaScript and JSON, I think all object literal member ids are unquoted strings. So, I would expect the result to be {myUrl:"http://example.com"}.

This seems too big a bug in PHP, so I must be wrong.

Edit, 2/11/11: Yes, I'm wrong. JSON syntax requires even the field names to be in double quotation marks.

Related