Why does JSON encoder adds escaping character when encoding URLs?

后端 未结 5 2049
太阳男子
太阳男子 2020-12-17 17:15

I am using json_encode in PHP to encode an URL

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


        
5条回答
  •  北海茫月
    2020-12-17 17:25

    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.

提交回复
热议问题