开发者

trouble parsing yahoo API json with jquery

开发者 https://www.devze.com 2023-01-05 11:09 出处:网络
I\'m having trouble parsing the following link. I\'d like to be able to extract a few of the object characteristics from each object in the array using $.getJSON(). Does anyone have a clue how I can d

I'm having trouble parsing the following link. I'd like to be able to extract a few of the object characteristics from each object in the array using $.getJSON(). Does anyone have a clue how I can do this?

Thanks!

http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=market&results=2&language=en&output=json&callback=?

Here is the actual object:

{
  "ResultSet": {
    "totalResultsAvailable": "68369",
    "totalResultsReturned": 2,
    "firstResultPosition": "1",
    "Result": [{
      "Title": "MARKET SNAPSHOT: U.S. Stocks To Begin New Week In Vulnerable Spot",
      "Summary": "MARKET SNAPSHOT: U.S. Stocks To Begin New Week In Vulnerable Spot",
      "Url": "http:\/\/www.foxbusiness.com\/story\/markets\/i开发者_如何学运维ndustries\/market-snapshot-stocks-begin-new-week-vulnerable-spot\/",
      "ClickUrl": "http:\/\/www.foxbusiness.com\/story\/markets\/industries\/market-snapshot-stocks-begin-new-week-vulnerable-spot\/",
      "NewsSource": "FOX Business",
      "NewsSourceUrl": "http:\/\/www.foxbusiness.com\/",
      "Language": "en",
      "PublishDate": "1278143248",
      "ModificationDate": "1278144826"
    }, {
      "Title": "MARKET SNAPSHOT: U.S. Stocks In The Red, Post Weekly Losses",
      "Summary": "MARKET SNAPSHOT: U.S. Stocks In The Red, Post Weekly Losses",
      "Url": "http:\/\/feeds.foxbusiness.com\/~r\/foxbusiness\/latest\/~3\/hL3f6RiYhdU\/",
      "ClickUrl": "http:\/\/feeds.foxbusiness.com\/~r\/foxbusiness\/latest\/~3\/hL3f6RiYhdU\/",
      "NewsSource": "Fox News",
      "NewsSourceUrl": "http:\/\/www.foxnews.com\/",
      "Language": "en",
      "PublishDate": "1278109361",
      "ModificationDate": "1278109412"
    }]
  }
}


I'm not sure I get it completely, but if you read closely, you'll see it's on object notation (that's what JSON means, after all), so you can access any property with a qualified name (data.ResultSet.Result[0].Summary would access the summary of the first result, par example).

Anyway, which characteristics would you like to extract? Would you like to print 'em to some DOM component, alert them or store them in a variable? Whatever the case, check this code out: for each result, it will print it's title and url in an alert dialog, (it uses jQuery's each function to iterate over the results):

$.getJSON('http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=market&results=2&language=en&output=json&callback=',
function(data){
   $.each(data.ResultSet.Result, function(index, value){
     alert("Result #"+index+": "+value.Title+" url: "+value.Url);
   });
   return false;
});

I'd leave the callback because it could be a cross site call, and that parameter is used to circumvent the same origin policy enforced by most major browsers.


The following should work. You will need to extract the properties you want from data

$.getJSON('http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=market&results=2&language=en&output=json&callback=', function(data) {
  alert(data.ResultSet.totalResultsAvailable);
});

UPDATE

Outputs a meaningful result, callback is required for success function to fire.

0

精彩评论

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