开发者

How to parse JSONP data returned from remote server

开发者 https://www.devze.com 2023-01-13 03:07 出处:网络
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 nest

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 f开发者_JS百科unction. 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.


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"
}


Write a function with the correct name and the correct arguments. The JS engine will do the parsing for you.

function someFunction(data) {
    // Now data is an Array, containing a single
    // Object with 8 properties (title, link, etc)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消