【问题标题】:How to parse this JSON in Javascript?如何在 Javascript 中解析这个 JSON?
【发布时间】:2013-04-29 14:39:50
【问题描述】:

寻求一些指导。我正在按照本指南构建我自己的 Google Chrome 扩展程序。

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

在他们的示例中效果很好,但是当我尝试将其应用于我自己的 JSON 数据时,它不起作用。

他们的例子:

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);

样本数据notifications.json如下:

[
    {"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"} 
]

非常直接。它从notifications.json 获取数据,并且基本上对结果执行foreach 以寻找新的message 标记。如果它找到一个,它会在扩展徽章上设置 NEW。

当然,我想将$.getJSON('http://dl.dropbox.com/u/31988864/notifications.json', 更改为我自己的 RSS 提要,这是在 Google 的帮助下转换为 JSON 完成的。

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

这是一个输出示例。比较长,所以我只提供一个sn-p。

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

我想我可以用 Google 的 contentSnippet 部分替换示例代码的 message

但是,这不起作用,这是我的问题:

我将如何进行这项工作?我已经尝试过val['responseData.feed.contentSnippet']val['feed.contentSnippet'],甚至只是在下面的部分中尝试了val['contentSnippet'],但它们都不起作用。

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

最终,我希望能够将单词 NEW 更改为实际的未读计数。但我认为我需要开始进行背景调查。

【问题讨论】:

    标签: javascript json google-chrome-extension


    【解决方案1】:

    您需要迭代的是 responseData/feed/entries,所以:

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

    【讨论】:

    • 谢谢。还是行不通。我会将 if 语句更改为 if (messages.indexOf(entry['contentSnippet']) == -1){ 吗?
    • (messages.indexOf(entry.contentSnippet) &lt; 0)
    • 很奇怪。使用示例数据,chrome 徽章将在刷新时显示 NEW。我没有得到任何东西,这让我相信它仍然无法正常工作。
    • 它适用于我 CodePen 或 JS 控制台(一旦我在 Chrome 中禁用了网络安全);相同的代码应该在扩展中可以正常工作。运行扩展程序时,您是否在 Javascript 控制台中看到错误?
    • 好点。控制台中没有错误。老实说,我对 JS/JSON 并不精通,所以我希望对示例扩展进行一些小的更改来解决它,但似乎并非如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2014-04-16
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多