Reputation:
Can someone explain to me how this parse function actually works?
function parseFlickrJson(jsonstring){
var data=null;
var jsonFlickrApi=function(d){
data = d;
}
eval(jsonstring);
return data;
}
Upvotes: 3
Views: 591
Reputation: 38382
JSON is actually valid JavaScript. So all you need to 'decode' it, is to evaluate it as JavaScript (hence the eval
). It's also using something known as JSONP http://en.wikipedia.org/wiki/JSONP where more than just JSON is returned.
JSONP is basically JSON wrapped in a function call. The content of a JSONP response might be:
parseResponse({"Name": "Cheeso", Id : 1823, "Rank": 7})
What this means is that when you evaluate JSONP, it will attempt to call a function (in this example parseResponse
and in your case jsonFlickrApi
). This is why the jsonFlickrApi
function must be defined before the eval(jsonstring)
occurs.
Upvotes: 7