简体   繁体   中英

How to parse this JSON in Javascript?

Looking for a little guidance. I'm following this guide to build my own Google Chrome extension.

http://www.seomoz.org/blog/building-chrome-apps-and-extensions

It works great with their examples, but when I try to apply it to my own JSON data, it's not working.

Their example:

var messages = [];
var whens = [];

function checkNotifications(){
  $.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',function(data){
    var new_messages = [];
    var new_whens = [];
    $.each(data, function(key, val) {
      if (messages.indexOf(val['message']) == -1){
        chrome.browserAction.setBadgeText({'text': 'New'});
        chrome.browserAction.setBadgeBackgroundColor({'color': '#f00'});
      }
      new_messages.push(val['message']);
      new_whens.push(val['when']);
    });
    messages = new_messages;
    whens = new_whens;
  });
}

checkNotifications();
// 1800000 milliseconds is 30 minutes
setInterval(checkNotifications,1800000);

The sample data notifications.json looks like:

[
    {"message": "<b>New blog post</b>: by David Sottimano - <a href='http://www.distilled.net/blog/miscellaneous/proxy-server-essential-information-for-seos/'>Proxy server essential information for SEOs</a>", "when": "6 days ago"},
    {"message": "<b>New module released</b>: Further SEO - <a href='http://www.distilled.net/u/linkbait/'>Linkbait that gets links</a>", "when": "11 days ago"},
    {"message": "<b>New blog post</b>: by Benjamin Estes - <a href='http://www.distilled.net/blog/web-analytics/segmenting-keywords-using-seotools/'>Segmenting keywords using SeoTools</a>", "when": "14 days ago"},
    {"message": "<b>New blog post</b>: by Will Critchlow - <a href='http://www.distilled.net/blog/conversion-rate-optimization/why-your-cro-tests-fail/'>Why your CRO tests fail</a>", "when": "16 days ago"} 
]

Very straight forward. It gets the data from notifications.json , and essentially does a foreach on the results looking for new a message tag. If it finds one, it sets NEW on the extension badge.

Naturally I want to change the $.getJSON('http://dl.dropbox.com/u/31988864/notifications.json', to my own RSS feed, which is done with a little help from Google to convert to JSON.

https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/thegearpost

Here's a sample of the output. It's rather long, so I'm only providing a snippet.

{
  "responseData": 
  {
    "feed":
    {
      "feedUrl":"http://feeds.feedburner.com/thegearpost",
      "title":"TheGearPost",
      "link":"http://thegearpost.com",
      "author":"",
      "description":"The online gadget, gear and gift guide for men.",
      "type":"rss20",
      "entries":
      [
        {
          "title":"Man Crates",
          "link":"http://feedproxy.google.com/~r/thegearpost/~3/_WO8mJS7dUY/",
          "author":"Pat",
          "publishedDate":"Fri, 03 May 2013 12:19:10 -0700",
          "contentSnippet":"Call in your own care package with Man Crates."

I'm thinking I can replace the sample code's message with Google's contentSnippet section.

However, this doesn't work, and this is my question:

How would I make this work? I've tried val['responseData.feed.contentSnippet'] , val['feed.contentSnippet'] , and even just val['contentSnippet'] in the below section, but none of it is working.

$.each(data, function(key, val) {
  if (messages.indexOf(val['responseData.feed.contentSnippet']) == -1){
    chrome.browserAction.setBadgeText({'text': 'New'});
    chrome.browserAction.setBadgeBackgroundColor({'color': '#f00'});
  }
});

Ultimately I'd like to be able to change the words NEW to an actual unread count. But I think I need to get the background checks working to begin with.

The thing you need to iterate over is responseData/feed/entries, so:

$.each(data.responseData.feed.entries, 
  function(key, entry) {
    var snippet = entry.contentSnippet;
    // whatever you need to do
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM