-2

我正在尝试在 php 中生成 JSON。我的问题是一些反斜杠保留在 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';
$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 字符串?

4

2 回答 2

5

看起来您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';
echo file_get_contents($jsonurl);
于 2012-09-09T20:47:32.037 回答
1

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

于 2012-09-09T20:49:48.200 回答