¿Cómo analizar este JSON en Javascript?

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


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

Su ejemplo:

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

function checkNotifications(){
    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'});
    messages = new_messages;
    whens = new_whens;

// 1800000 milliseconds is 30 minutes

The sample data notifications.json parece:

    {"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.


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

      "description":"The online gadget, gear and gift guide for men.",
          "title":"Man Crates",
          "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 con Google contentSnippet .

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'], e incluso solo 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.

preguntado el 05 de mayo de 13 a las 21:05

1 Respuestas

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

  function(key, entry) {
    var snippet = entry.contentSnippet;
    // whatever you need to do

contestado el 05 de mayo de 13 a las 21:05

Thanks. Still not working. Would I change the if statement to if (messages.indexOf(entry['contentSnippet']) == -1){ ? - Palmadita

or (messages.indexOf(entry.contentSnippet) < 0) - Pablo Roub

Weird. With the sample data, the chrome badge would show NEW on refresh. I'm not getting anything, which leads me to believe it's still not working. - Palmadita

It works fine for me CodePen or the JS console (once I disabled web security in Chrome); the same code debemos work fine in an Extension. Are you seeing errors in the Javascript console when running your extension? - Pablo Roub

Good point. No errors in the console. I'm not well versed in JS/JSON to be honest, so I was hoping some minor changes to the sample extension would fix it up, but seems to be not the case. - Palmadita

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.