json_encode不会删除反斜杠

时间:2012-09-09 20:45:16

标签: php json

我正在尝试在php中生成JSON。我的问题是,即使我使用json_encode

,一些反斜杠也会保留在json字符串中
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$jsonurl = 'http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=e6c27be80b3b54c55376a5f59009a8db&photo_id=7962163506&format=json&nojsoncallback=1';
$json = file_get_contents($jsonurl);

echo json_encode($json);

结果:

"{\"sizes\":{\"canblog\":0, \"canprint\":0, \"candownload\":1, \"size\":[{\"label\":\"Square\", \"width\":75, \"height\":75, \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_s.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/sq\\\/\", \"media\":\"photo\"}, {\"label\":\"Large Square\", \"width\":\"150\", \"height\":\"150\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_q.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/q\\\/\", \"media\":\"photo\"}, {\"label\":\"Thumbnail\", \"width\":\"100\", \"height\":\"66\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_t.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/t\\\/\", \"media\":\"photo\"}, {\"label\":\"Small\", \"width\":\"240\", \"height\":\"159\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_m.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/s\\\/\", \"media\":\"photo\"}, {\"label\":\"Small 320\", \"width\":\"320\", \"height\":212, \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_n.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/n\\\/\", \"media\":\"photo\"}, {\"label\":\"Medium\", \"width\":\"500\", \"height\":\"331\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/m\\\/\", \"media\":\"photo\"}, {\"label\":\"Medium 640\", \"width\":\"640\", \"height\":\"424\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_z.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/z\\\/\", \"media\":\"photo\"}, {\"label\":\"Medium 800\", \"width\":\"800\", \"height\":530, \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_c.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/c\\\/\", \"media\":\"photo\"}, {\"label\":\"Large\", \"width\":\"1024\", \"height\":\"678\", \"source\":\"http:\\\/\\\/farm9.staticflickr.com\\\/8318\\\/7962163506_139d6bf0a0_b.jpg\", \"url\":\"http:\\\/\\\/www.flickr.com\\\/photos\\\/87015633@N08\\\/7962163506\\\/sizes\\\/l\\\/\", \"media\":\"photo\"}]}, \"stat\":\"ok\"}"

预期结果:

{"sizes":{"canblog":0, "canprint":0, "candownload":1, "size":[{"label":"Square", "width":75, "height":75, "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_s.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/sq\/", "media":"photo"}, {"label":"Large Square", "width":"150", "height":"150", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_q.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/q\/", "media":"photo"}, {"label":"Thumbnail", "width":"100", "height":"66", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_t.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/t\/", "media":"photo"}, {"label":"Small", "width":"240", "height":"159", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_m.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/s\/", "media":"photo"}, {"label":"Small 320", "width":"320", "height":212, "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_n.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/n\/", "media":"photo"}, {"label":"Medium", "width":"500", "height":"331", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/m\/", "media":"photo"}, {"label":"Medium 640", "width":"640", "height":"424", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_z.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/z\/", "media":"photo"}, {"label":"Medium 800", "width":"800", "height":530, "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_c.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/c\/", "media":"photo"}, {"label":"Large", "width":"1024", "height":"678", "source":"http:\/\/farm9.staticflickr.com\/8306\/7962671096_61367d4804_b.jpg", "url":"http:\/\/www.flickr.com\/photos\/87015633@N08\/7962671096\/sizes\/l\/", "media":"photo"}]}, "stat":"ok"}

我需要更改什么才能获得正确的JSON字符串?

2 个答案:

答案 0 :(得分:5)

看起来你在JSON字符串上调用json_encode,只是输出它。

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$jsonurl = 'http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=e6c27be80b3b54c55376a5f59009a8db&photo_id=7962163506&format=json&nojsoncallback=1';
echo file_get_contents($jsonurl);

答案 1 :(得分:1)

你已经有了一个JSON字符串,不需要再次调用json_encode,当你有一些数据结构并希望序列化为JSON时应该使用该函数,在这种情况下你可以直接输出