我的 json_decode 返回 null,我想我在发布这篇文章之前已经尝试了所有方法。请给我提意见。
我使用 Windows Live REST API 来读取用户相册和照片。json_decode 非常适合解码所有其他 Windows 实时端点的响应,但不能解码来自这个端点的响应:(...
$albums_url = "https://apis.live.net/v5.0/".$albumId."/photos?access_token=" . urlencode($accessToken);
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init($albums_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, "my web site domain name here");
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
$albumPhotosResponse=curl_exec($ch);
curl_close($ch);
当 json 响应到达时,它包含很多 \r 和 \n 。甚至是字里行间。所以我把它全部清理干净。
1.修剪
$albumPhotosResponse = trim($albumPhotosResponse);
2.删除所有\n
和\r
$albumPhotosResponse = str_replace(array("\n", "\r"),"",$albumPhotosResponse);
3.为了确保没有多余的空格,我这样做
$albumPhotosResponse = preg_replace('!\s+!', '', $albumPhotosResponse);
我搜索了一下,发现对于许多开发人员来说,问题json
在于没有UTF-8
编码值。所以我也试过这个。
$photoAlbumsResponse = json_decode(utf8_encode($photoAlbumsResponse),true);
Chrome 会正确显示和格式化收到的 json 文档。这告诉我 json 文档的结构没有问题。我可以使用可视化工具折叠和展开 json 对象。json 是正确的。但是当我尝试json_decode()
它时。它返回空:(
请告诉我如何解决这个问题。
谢谢你。
这是 JSON 文档本身:
{
"data": [
{
"id": "file.f6099d461e678395.F6099D461E678395!126",
"from": {
"name": "SergeyKargopolov",
"id": "f6099d461e678395"
},
"name": "IMG_5398_small.jpg",
"description": "",
"parent_id": "folder.f6099d461e678395.F6099D461E678395!125",
"size": 84683,
"comments_count": 0,
"comments_enabled": true,
"tags_count": 0,
"tags_enabled": true,
"picture": "https://public.blu.livefilestore.com/y2mkGF4H0SO63lkIzy-uMOV5AES5ndgqynGmJwWGUOfkmdVsEU80dW2UnbDcF3_EoYpV8oFH6UB_AxnDZ8hmOud_AvdU65BSPJvE8yYfACcWYZyab2cqRo_gk2S851c89Fb/IMG_5398_small.jpg?psid=1",
"source": "https://public.blu.livefilestore.com/y2mcTvD8EbPp9R2oBWP446Jp40SZmDrNQ5Cc1ssIIwTN5f9btKk3NnykeK10-CXBakQ0D4jRz9ugXpJSVerTdZ4JfA9UOkemKB75IDO9pkL4IrvtiDpyTj5dzf4kAGolzkc/IMG_5398_small.jpg?psid=1",
"images": [
{
"height": 800,
"width": 533,
"source": "https://public.blu.livefilestore.com/y2m-fnaGh1zrR0sLanBP0D_wzT4ocJTqKE2A_8rb8n6_U_FlMt8Rp_dhoEuDBE4f1nkG3ApxWndOsrPfxB-phxJ7-4O343mS5I5n67zE2ZbfI2F_a-SJFhlL7_QcXsobhvU/IMG_5398_small.jpg?psid=1",
"type": "normal"
},
{
"height": 176,
"width": 117,
"source": "https://public.blu.livefilestore.com/y2my2M-gI6cZv33A0ueydYDxackIY4w7vqhmxksbZuBddvZXHsPykgivJTio_VlnSP0hOXu8eKRwRXRfnGK6Flk6HLhsFE4XlJjPyhc9pQ9IdSGV3F7JYZQKxZ7Pcp3Y3Cb/IMG_5398_small.jpg?psid=1",
"type": "album"
},
{
"height": 96,
"width": 63,
"source": "https://public.blu.livefilestore.com/y2mkGF4H0SO63lkIzy-uMOV5AES5ndgqynGmJwWGUOfkmdVsEU80dW2UnbDcF3_EoYpV8oFH6UB_AxnDZ8hmOud_AvdU65BSPJvE8yYfACcWYZyab2cqRo_gk2S851c89Fb/IMG_5398_small.jpg?psid=1",
"type": "thumbnail"
},
{
"height": 900,
"width": 600,
"source": "https://public.blu.livefilestore.com/y2mcTvD8EbPp9R2oBWP446Jp40SZmDrNQ5Cc1ssIIwTN5f9btKk3NnykeK10-CXBakQ0D4jRz9ugXpJSVerTdZ4JfA9UOkemKB75IDO9pkL4IrvtiDpyTj5dzf4kAGolzkc/IMG_5398_small.jpg?psid=1",
"type": "full"
}
],
"link": "https://skydrive.live.com/redir.aspx?cid=f6099d461e678395&page=view&resid=F6099D461E678395!126&parid=F6099D461E678395!125",
"when_taken": null,
"height": 900,
"width": 600,
"type": "photo",
"camera_make": null,
"camera_model": null,
"focal_ratio": 0,
"focal_length": 0,
"exposure_numerator": 0,
"exposure_denominator": 0,
"created_time": "2010-07-27T04:19:59+0000",
"updated_time": "2010-07-27T04:20:00+0000"
},
{
"id": "file.f6099d461e678395.F6099D461E678395!131",
"from": {
"name": "SergeyKargopolov",
"id": "f6099d461e678395"
},
"name": "011s769f.jpg",
"description": "",
"parent_id": "folder.f6099d461e678395.F6099D461E678395!125",
"size": 96091,
"comments_count": 0,
"comments_enabled": true,
"tags_count": 0,
"tags_enabled": true,
"picture": "https://public.blu.livefilestore.com/y2m5gYxO0yiumEjjK29fEI7AaLP8kCKmHVSU5UI7XWb-l2z0uZRfCyHBVujSkAQhjjeZHww1sqfLlsg5nY4jUpfkgcTibKw7xc5Qfi3zcHkB22BmG-gMyZi8xDjM0XXWOdA/011s769f.jpg?psid=1",
"source": "https://public.blu.livefilestore.com/y2mZicAQqNBndn2-DlU0uAh1yK5AgoB2G2zqBnVr2nXVlDgs9nxk441RPuHa8sqUHeCk4cRK6lhfPDi9fcbVRVBlapQ9w_xSh4I86E0v9hE1368sIPGrHEH4pNFqs4CA7Y-/011s769f.jpg?psid=1",
"images": [
{
"height": 800,
"width": 529,
"source": "https://public.blu.livefilestore.com/y2mNp-KCqcEvWN0lYYO129mMIw8AmcCccv9rG8RawrQort6wNapXghZVlxHKLngDwJxo4P-3gdzHSPxlfpKVFgdvQJY_sVC-o3El9k9nkNFpop162qGlEAvooNvlqIAlaPH/011s769f.jpg?psid=1",
"type": "normal"
},
{
"height": 176,
"width": 116,
"source": "https://public.blu.livefilestore.com/y2mJKrnTHXH6yWv4Eb9lMpqDBED7QSVaoc6Fs1PHPtiqijK8iw7A0l2e9gOMvRvG0esCVWQbG3dtUVS1r0nXjabjG1xikqc586nf2GBeNMmSZYRDWa143yRLUkU5uldZ7ko/011s769f.jpg?psid=1",
"type": "album"
},
{
"height": 96,
"width": 63,
"source": "https://public.blu.livefilestore.com/y2m5gYxO0yiumEjjK29fEI7AaLP8kCKmHVSU5UI7XWb-l2z0uZRfCyHBVujSkAQhjjeZHww1sqfLlsg5nY4jUpfkgcTibKw7xc5Qfi3zcHkB22BmG-gMyZi8xDjM0XXWOdA/011s769f.jpg?psid=1",
"type": "thumbnail"
},
{
"height": 1024,
"width": 678,
"source": "https://public.blu.livefilestore.com/y2mZicAQqNBndn2-DlU0uAh1yK5AgoB2G2zqBnVr2nXVlDgs9nxk441RPuHa8sqUHeCk4cRK6lhfPDi9fcbVRVBlapQ9w_xSh4I86E0v9hE1368sIPGrHEH4pNFqs4CA7Y-/011s769f.jpg?psid=1",
"type": "full"
}
],
"link": "https://skydrive.live.com/redir.aspx?cid=f6099d461e678395&page=view&resid=F6099D461E678395!131&parid=F6099D461E678395!125",
"when_taken": "2010-10-30T12:08:10+0000",
"height": 1024,
"width": 678,
"type": "photo",
"camera_make": null,
"camera_model": null,
"focal_ratio": 0,
"focal_length": 0,
"exposure_numerator": 0,
"exposure_denominator": 0,
"created_time": "2010-11-10T21:34:19+0000",
"updated_time": "2010-11-12T21:27:17+0000"
},
{
"id": "file.f6099d461e678395.F6099D461E678395!132",
"from": {
"name": "SergeyKargopolov",
"id": "f6099d461e678395"
},
"name": "0111facs.jpg",
"description": "",
"parent_id": "folder.f6099d461e678395.F6099D461E678395!125",
"size": 103077,
"comments_count": 0,
"comments_enabled": true,
"tags_count": 0,
"tags_enabled": true,
"picture": "https://public.blu.livefilestore.com/y2m6gsOPgubuCzBpTvBOfyuYUiY33ToLDcl1J7IFPTkSBCdtd7AxIgdTr22k0LXoqhwzL3d7ZPESa3umbHHp37gNCEyoygH8WXnxOnNWW1xt_huFbcu1Au8kl5eF_bhvM-I/0111facs.jpg?psid=1",
"source": "https://public.blu.livefilestore.com/y2m3qQ0ew5NYVPazHoNNYlSdZszzzJCIVmH-6pSBwAAhmsruUq6BmciwF2Hnxv7h7fv2jdjPqPWKjs6AQ-_NuQrW2CnFMJ27k6A_DULXRwhr1K-fZr2LN0xLiIAocHL9gtk/0111facs.jpg?psid=1",
"images": [
{
"height": 540,
"width": 800,
"source": "https://public.blu.livefilestore.com/y2mfTbhvgI9mawwJUXWqzPWFLUDfwmBECiF7OYMXV5etprNG7gre5i7FQSQTZ3r8Oe9MfsmUKK40-EQLTOUlUHGuRz4Es1b_stSXuHIYDwJYeJ-AvcDe4SLFhx5G7lYacV1/0111facs.jpg?psid=1",
"type": "normal"
},
{
"height": 118,
"width": 176,
"source": "https://public.blu.livefilestore.com/y2m0vrDF8_R1FoZcEmgwBpkWcPWu902jJLlKzGOv4R4UIQvMHbSHNbLLAyUrKJagbHypqxZeUaQpwJXayLUxCExKEzsWBF0pD0_pWNDfk9NmfDlB_dtU3y0PlNIIATuSoTC/0111facs.jpg?psid=1",
"type": "album"
},
{
"height": 64,
"width": 96,
"source": "https://public.blu.livefilestore.com/y2m6gsOPgubuCzBpTvBOfyuYUiY33ToLDcl1J7IFPTkSBCdtd7AxIgdTr22k0LXoqhwzL3d7ZPESa3umbHHp37gNCEyoygH8WXnxOnNWW1xt_huFbcu1Au8kl5eF_bhvM-I/0111facs.jpg?psid=1",
"type": "thumbnail"
},
{
"height": 608,
"width": 900,
"source": "https://public.blu.livefilestore.com/y2m3qQ0ew5NYVPazHoNNYlSdZszzzJCIVmH-6pSBwAAhmsruUq6BmciwF2Hnxv7h7fv2jdjPqPWKjs6AQ-_NuQrW2CnFMJ27k6A_DULXRwhr1K-fZr2LN0xLiIAocHL9gtk/0111facs.jpg?psid=1",
"type": "full"
}
],
"link": "https://skydrive.live.com/redir.aspx?cid=f6099d461e678395&page=view&resid=F6099D461E678395!132&parid=F6099D461E678395!125",
"when_taken": null,
"height": 608,
"width": 900,
"type": "photo",
"camera_make": null,
"camera_model": null,
"focal_ratio": 0,
"focal_length": 0,
"exposure_numerator": 0,
"exposure_denominator": 0,
"created_time": "2010-11-10T21:34:21+0000",
"updated_time": "2010-11-10T21:34:21+0000"
},
{
"id": "file.f6099d461e678395.F6099D461E678395!133",
"from": {
"name": "SergeyKargopolov",
"id": "f6099d461e678395"
},
"name": "0111tcez.jpg",
"description": "",
"parent_id": "folder.f6099d461e678395.F6099D461E678395!125",
"size": 92916,
"comments_count": 0,
"comments_enabled": true,
"tags_count": 0,
"tags_enabled": true,
"picture": "https://public.blu.livefilestore.com/y2m1cj77LH9hzorTOA0jZ16Np2J5-Ut9_9VwZKQ-Er9lXfDcgc6iF1gfQzVEzgLIEqd5Q_gxKAdyJFil3JB9G7ku6eMJZCplG6dz1UQcU4IkwIrI_U8f8y04Hyy_Dj2oTy2/0111tcez.jpg?psid=1",
"source": "https://public.blu.livefilestore.com/y2mqv57qXM8TmrOqmJVkPohDxtApxRsWRIuzQYDd2JT_8KbO5bv72yUE_UVX-Z5-xA0eSYXqz2x42s314MOq8xc7lyi7cuaFNgEV1KupIfJ8R0K5xF1xRwdVxL4b010c8r-/0111tcez.jpg?psid=1",
"images": [
{
"height": 800,
"width": 554,
"source": "https://public.blu.livefilestore.com/y2mdRIJj3A6tNLVr6cXwGRJZeDDbkI78YEujBv_i5UmBCoLYnqBm2Iv06W_KlZIXNeBKby3Z1y_Oeb2tVUTmt4gBA/0111tcez.jpg?psid=1&ck=16&ex=720",
"type": "normal"
},
{
"height": 176,
"width": 121,
"source": "https://public.blu.livefilestore.com/y2mjb6q9PCcQuUEG8qDNw8wri6Slsj5SjiQz7qJuk235sNX7a0z0WcW25X2Q11dcSrJsssTWtDYZgq6hKFlon3SDxKELEnThqN0Xq5y6Ia7XgPHiTiLQngjRaXiKCpHfhQu/0111tcez.jpg?psid=1",
"type": "album"
},
{
"height": 96,
"width": 66,
"source": "https://public.blu.livefilestore.com/y2m1cj77LH9hzorTOA0jZ16Np2J5-Ut9_9VwZKQ-Er9lXfDcgc6iF1gfQzVEzgLIEqd5Q_gxKAdyJFil3JB9G7ku6eMJZCplG6dz1UQcU4IkwIrI_U8f8y04Hyy_Dj2oTy2/0111tcez.jpg?psid=1",
"type": "thumbnail"
},
{
"height": 900,
"width": 624,
"source": "https://public.blu.livefilestore.com/y2mqv57qXM8TmrOqmJVkPohDxtApxRsWRIuzQYDd2JT_8KbO5bv72yUE_UVX-Z5-xA0eSYXqz2x42s314MOq8xc7lyi7cuaFNgEV1KupIfJ8R0K5xF1xRwdVxL4b010c8r-/0111tcez.jpg?psid=1",
"type": "full"
}
],
"link": "https://skydrive.live.com/redir.aspx?cid=f6099d461e678395&page=view&resid=F6099D461E678395!133&parid=F6099D461E678395!125",
"when_taken": null,
"height": 900,
"width": 624,
"type": "photo",
"camera_make": null,
"camera_model": null,
"focal_ratio": 0,
"focal_length": 0,
"exposure_numerator": 0,
"exposure_denominator": 0,
"created_time": "2010-11-10T21:34:21+0000",
"updated_time": "2010-11-10T21:34:22+0000"
},
{
"id": "file.f6099d461e678395.F6099D461E678395!142",
"from": {
"name": "SergeyKargopolov",
"id": "f6099d461e678395"
},
"name": "011xfx15.jpg",
"description": "ALLURE/",
"parent_id": "folder.f6099d461e678395.F6099D461E678395!125",
"size": 183605,
"comments_count": 0,
"comments_enabled": true,
"tags_count": 0,
"tags_enabled": true,
"picture": "https://public.blu.livefilestore.com/y2mnESJYaEhmj46Iq_sDjjZVgrs8636E_DuHdGHMVEJh558hgoc1KzIwGjckWLRhieCuG_wpq2KZ0EYQxkONF3Pe3ID5eCz2IiVOlrFp4p2U1hC1jQ4-z-DQ3hragV2otEg/011xfx15.jpg?psid=1",
"source": "https://public.blu.livefilestore.com/y2mNGQLRETzgJSL4go-5FJ2rtDayq-rOCcD5CphPiSLoXZNn1t_w3uGr-mVMvuhVdJVIFrBLtvKL-wXFc7Pwk5MMCHJwZsZ0t_xOVXLXKq6eSfjYanu0xnCuvnFwB1H5bwq/011xfx15.jpg?psid=1",
"images": [
{
"height": 496,
"width": 800,
"source": "https://public.blu.livefilestore.com/y2muc04ASn8PnEpsbLKR1t85o7ehGUQg5Q5ZMkjvQ14wPqrS-ycSGnSa-ujmrtB9JRSGBx_BlE3E36EfZmRrkQd9g/011xfx15.jpg?psid=1&ck=16&ex=720",
"type": "normal"
},
{
"height": 108,
"width": 176,
"source": "https://public.blu.livefilestore.com/y2mnJNZVApUSdeKRGH6TDT4BJ_eNmHeVKhcdZUsjFyaQAXzL-CkAaiSADroRCylRVMf7u7iFv6wse7CPhc6ijZMwSX5eGEZntFzFHPvZyVIpcd1U-nmW8_ckzFIPFmhd2NW/011xfx15.jpg?psid=1",
"type": "album"
},
{
"height": 58,
"width": 96,
"source": "https://public.blu.livefilestore.com/y2mnESJYaEhmj46Iq_sDjjZVgrs8636E_DuHdGHMVEJh558hgoc1KzIwGjckWLRhieCuG_wpq2KZ0EYQxkONF3Pe3ID5eCz2IiVOlrFp4p2U1hC1jQ4-z-DQ3hragV2otEg/011xfx15.jpg?psid=1",
"type": "thumbnail"
},
{
"height": 682,
"width": 1100,
"source": "https://public.blu.livefilestore.com/y2mNGQLRETzgJSL4go-5FJ2rtDayq-rOCcD5CphPiSLoXZNn1t_w3uGr-mVMvuhVdJVIFrBLtvKL-wXFc7Pwk5MMCHJwZsZ0t_xOVXLXKq6eSfjYanu0xnCuvnFwB1H5bwq/011xfx15.jpg?psid=1",
"type": "full"
}
],
"link": "https://skydrive.live.com/redir.aspx?cid=f6099d461e678395&page=view&resid=F6099D461E678395!142&parid=F6099D461E678395!125",
"when_taken": "2010-11-11T21:07:02+0000",
"height": 682,
"width": 1100,
"type": "photo",
"camera_make": null,
"camera_model": null,
"focal_ratio": 0,
"focal_length": 0,
"exposure_numerator": 0,
"exposure_denominator": 0,
"created_time": "2010-11-15T21:48:22+0000",
"updated_time": "2010-11-15T21:48:23+0000"
}
]
}