English 中文(简体)
将体力转换为 j子
原标题:convert string to JSON with jQuery

I am getting data from the Flickr API with a middle-man file (to avoid crossdomain problems):

<?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 ]) ) );
?>

This file is fetched by javascript:

//Flickr
var myurl = encodeURIComponent( http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=1408bff5f72a4b84b924d13e8562b6a2&photoset_id=77649470@N03&photoset_id=72157629903184261&format=json );
    $.getJSON( "middle.php?url=" + myurl, function(data){
        console.log(typeof data);
    });

但是,这只是ole。 记录显示,结果是一种“扼杀”,而不是“焦耳”。 Ive试图将其改装成一个有:

jQuery.parseJSON(data)

but the console gave me this error:

Uncaught SyntaxError: Unexpected token j

This is the string:

jsonFlickrApi({"photoset":{"id":"72157629903184261", "primary":"7115173307", "owner":"77649470@N03", "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"})

我可以做些什么来把扼杀变成物体?

最佳回答

有些人把回答说了,但出于某种原因,他们删除了这个职位,因此我在此复制:

$.ajax({
    url:  http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=1408bff5f72a4b84b924d13e8562b6a2&photoset_id=77649470@N03&photoset_id=72157629903184261&format=json ,
    type:  GET ,
    dataType:  jsonp ,
    jsonpCallback:  jsonFlickrApi ,
    success: function(data){
        console.log(data);
    }
});

这完全是工作。 感谢 任何人:

也许还有其他工作,但我感到更加舒适。 谢谢大家。

问题回答

The JSON starts with the object curly brace { The jsonFlickrApi( is not part of the JSON object. You could string replace data first to remove the jsonFlickrApi( part first if you only need the inner, but it seems like Flickr would have a better built-in solution.

标题9

这样,可以由联合材料作为普通的JSON数据加以分类。

正如上文已经提到的那样,Fakr正在将“JN”的复信发送到“密码>jsonFakrApi >的背靠。

为了归还纯粹的JSON数据,你可以使用nojsonert=1请求参数。

E.g. http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=_secret_app_key&photo_id=8321754843&format=json&nojsonertback=1





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签