2

我有一個Google Spreadsheet,我想用Python獲取,然後通過JSON處理它。這是中途工作,並通過Stackoverflow幾個小時後,我認爲是時候提出一個問題。在Python中迭代和打印JSON對象

例如,JSON文件的格式如下所示(來自https://developers.google.com/gdata/docs/json)。

{ 
    "version": "1.0", 
    "encoding": "UTF-8", 
    "feed": { 
    "xmlns": "http://www.w3.org/2005/Atom", 
    "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/", 
    "xmlns$gd": "http://schemas.google.com/g/2005", 
    "xmlns$gCal": "http://schemas.google.com/gCal/2005", 
    "id": {"$t": "..."}, 
    "updated": {"$t": "2006-11-12T21:25:30.000Z"}, 
    "title": { 
     "type": "text", 
     "$t": "Google Developer Events" 
    }, 
    "subtitle": { 
     "type": "text", 
     "$t": "The calendar contains information about upcoming developer 
     conferences at which Google will be speaking, along with other 
     developer-related events." 
    }, 
    "link": [{ 
     "rel": "...", 
     "type": "application/atom+xml", 
     "href": "..." 
     },{ 
     "rel": "self", 
     "type": "application/atom+xml", 
     "href": "..." 
    }], 
    "author": [{ 
     "name": {"$t": "Google Developer Calendar"}, 
     "email": {"$t": "[email protected]"} 
    }], 
    "generator":{ 
     "version": "1.0", 
     "uri": "http://www.google.com/calendar", 
     "$t": "Google Calendar" 
    }, 
    "openSearch$startIndex": {"$t": "1"}, 
    "openSearch$itemsPerPage": {"$t": "25"}, 
    "gCal$timezone": {"value": "America/Los_Angeles"}, 

    "entry": [{ 
     "id": {"$t": "..."}, 
     "published": {"$t": "2006-11-12T21:25:30.000Z"}, 
     "updated": {"$t": "2006-11-12T21:25:30.000Z"}, 
     "category": [{ 
     "scheme": "...", 
     "term": "..." 
     }], 
     "title":{ 
     "type": "text", 
     "$t": "WebmasterWorld PubCon 2006: Google Developer Tools in General" 
     }, 
     "content": { 
     "type": "text", 
     "$t": "Google is sponsoring at 
      <a href=\"http://www.pubcon.com/\">WebmasterWorld PubCon 2006</a>. 
      \n Come and visit us at the booth or join us for an evening demo 
      reception where we will be talking \"5 ways to enhance your website 
      with Google Code\". \n After all, \n it is Vegas, baby! See you soon." 
     }, 
     "link": [{ 
     "rel": "alternate", 
     "type": "text/html", 
     "href": "...", 
     "title": "alternate" 
     },{ 
     "rel": "self", 
     "type": "application/atom+xml", 
     "href": "..." 
     }], 
     "author": [{ 
     "name": {"$t": "Google Developer Calendar"}, 
     "email": {"$t": "[email protected]"} 
     }], 
     "gd$transparency": {"value": "http://schemas.google.com/g/2005#event.opaque"}, 
     "gd$eventStatus": {"value": "http://schemas.google.com/g/2005#event.confirmed"}, 
     "gd$comments": {"gd$feedLink": {"href": "..."}}, 
     "gCal$sendEventNotifications": {"value": "true"}, 
     "gd$when": [{ 
     "startTime": "2006-11-15", 
     "endTime": "2006-11-17", 
     "gd$reminder": [{"minutes": "10"}] 
     }], 
     "gd$where": [{"valueString": "3150 Paradise Road,Las Vegas,NV 89109"}]}, 
    }] 
    } 
} 

我的Python代碼如下:

import requests, json 
r = requests.get('link-to-google-spreadsheet-json') 
j = r.json() 

測試JSON文件輸出的頂級層次結構如下:

>>> print j["version"] 
1.0 

但遍歷的對象,像這樣:

for feed in j["feed"]: 
    for entry in feed["entry"]: 
     for title in entry["title"]: 
      print title["$t"] 
     print 
    print 

給我以下錯誤:

Traceback (most recent call last): 
File "<console>", line 2, in <module> 
TypeError: string indices must be integers. 

它聽起來像是要從字符串打印單個字母,只要我給索引號。那麼如何解析JSON,以便正確輸出我想要的內容(例如feed - > entry - > title - > $ t)?

+0

我已經使用Google的示例發佈了JSON文件的格式,因此您可以在feed - > entry - > title - > $ t中看到我正在嘗試獲取$ t的值。 – AAA

+0

我完全忽略了> _>我刪除了我以前的評論。 – Natan

回答

10

j ['feed']是一本字典。你的代碼應該是這樣的:

for entry in j['feed']['entry']: 
    print entry['title']['$t'] 
+0

謝謝。你能介紹一下我如何訪問字典,而不是你生成的代碼嗎? – AAA

+0

當你迭代j ['feed']時,你遍歷它的關鍵字:category,updated,... – sneawo

0

feedtitle的字典沒有列出,所以你不應該有環路他們。您只需要一個for-loop用於入口列表。