[英]How to parse this JSON in Javascript?
尋找一點指導。 我正在按照本指南構建自己的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。
這是輸出示例。 它很長,所以我只提供一個片段。
{
"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."
我在想可以將示例代碼的message
替換為Google的contentSnippet
部分。
但是,這不起作用,這是我的問題:
我將如何進行這項工作? 我在val['feed.contentSnippet']
嘗試過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一詞更改為實際的未讀計數。 但是我認為我需要首先進行背景檢查。
您需要迭代的內容是responseData / feed / entries,因此:
$.each(data.responseData.feed.entries,
function(key, entry) {
var snippet = entry.contentSnippet;
// whatever you need to do
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.