我从一个中间人文件Flickr的API获取数据(以避免跨域问题):字符串转换为JSON使用jQuery
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
die(json_encode(file_get_contents($_REQUEST['url'])));
?>
这个文件是获取由JavaScript:
//Flickr
var myurl = encodeURIComponent('http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=1408bff5f72a4b84b924d13e8562b6a2&[email protected]&photoset_id=72157629903184261&format=json');
$.getJSON("middle.php?url=" + myurl, function(data){
console.log(typeof data);
});
但是console.log显示结果是“字符串”而不是JSON对象。香港专业教育学院试图把它与转换为一个JSON对象:
jQuery.parseJSON(data)
但控制台给了我这个错误:
Uncaught SyntaxError: Unexpected token j
这是字符串:
jsonFlickrApi({"photoset":{"id":"72157629903184261", "primary":"7115173307", "owner":"[email protected]", "ownername":"wedocommunication", "photo":[{"id":"7115173331", "secret":"24900ff306", "server":"5447", "farm":6, "title":"Lounge", "isprimary":"0"}, {"id":"7115173307", "secret":"3435f9a983", "server":"7256", "farm":8, "title":"Hofansicht", "isprimary":"1"}, {"id":"7115173379", "secret":"7747e50597", "server":"7278", "farm":8, "title":"Konfi", "isprimary":"0"}, {"id":"6969093048", "secret":"d4389bc0e4", "server":"7055", "farm":8, "title":"Lounge", "isprimary":"0"}, {"id":"6969093086", "secret":"8e7263005b", "server":"5152", "farm":6, "title":"Eingangsbereich", "isprimary":"0"}], "page":1, "per_page":500, "perpage":500, "pages":1, "total":"5"}, "stat":"ok"})
我能怎么做将字符串转换为对象?
作为@ocanal在他的回答中提到,你不需要这个代理 – chrismarx 2012-04-27 14:38:04
这也不是工作:http://pastebin.com/ZEKWTSCc我得不到输出 – 2012-04-27 14:45:40
它的工作原理是这样的 - http:///pastebin.com/n7WCJrGZ,看看为什么urs可以工作 – chrismarx 2012-04-27 15:01:30