How to parse JSONP data returned from remote server

patricksweeney picture patricksweeney · Aug 22, 2010 · Viewed 25.4k times · Source

I am trying to grab some data via JSONP. Using Firebug, I am able to see the data properly being returned, but I am having a hard time thinking how I have to parse it. The data return is really a nested array correct? someFunction is the name of the callback function. This is how the data looks:

someFunction([  
{  
       "title":"Sample Title",  
       "link":"http://example.com",  
       "description":"Sample Description",  
       "publisher":"Sample Publisher",  
       "creator":"Sample Author",  
       "date":"Thu, 19 Aug 2010 12:41:29 GMT",  
       "num_pages":10,  
       "num_results":"10"  
},  
]);

Just a little confused about how to properly parse and output.

Answer

Anurag picture Anurag · Aug 22, 2010

You don't have to parse the data. It is already a valid JavaScript object. For instance, to print the description property for the first object inside someFunction

function someFunction(result) {
    alert(result[0].description); // alerts "Sample Description"
}