我想遍歷一個JSON對象來導入數據,即標題和鏈接。我似乎無法獲得超過:
的內容。通過JSON對象迭代
JSON:
[
{
"title": "Baby (Feat. Ludacris) - Justin Bieber",
"description": "Baby (Feat. Ludacris) by Justin Bieber on Grooveshark",
"link": "http://listen.grooveshark.com/s/Baby+Feat+Ludacris+/2Bqvdq",
"pubDate": "Wed, 28 Apr 2010 02:37:53 -0400",
"pubTime": 1272436673,
"TinyLink": "http://tinysong.com/d3wI",
"SongID": "24447862",
"SongName": "Baby (Feat. Ludacris)",
"ArtistID": "1118876",
"ArtistName": "Justin Bieber",
"AlbumID": "4104002",
"AlbumName": "My World (Part II);\nhttp://tinysong.com/gQsw",
"LongLink": "11578982",
"GroovesharkLink": "11578982",
"Link": "http://tinysong.com/d3wI"
},
{
"title": "Feel Good Inc - Gorillaz",
"description": "Feel Good Inc by Gorillaz on Grooveshark",
"link": "http://listen.grooveshark.com/s/Feel+Good+Inc/1UksmI",
"pubDate": "Wed, 28 Apr 2010 02:25:30 -0400",
"pubTime": 1272435930
}
]
我試着使用的字典:
def getLastSong(user,limit):
base_url = 'http://gsuser.com/lastSong/'
user_url = base_url + str(user) + '/' + str(limit) + "/"
raw = urllib.urlopen(user_url)
json_raw= raw.readlines()
json_object = json.loads(json_raw[0])
#filtering and making it look good.
gsongs = []
print json_object
for song in json_object[0]:
print song
此代碼只打印:
之前的信息。 (忽略賈斯汀比伯軌道 :))
我不明白這一點。我試着重複你說的出界。我很確定它的一個問題關於json – myusuf3 2010-04-28 23:41:08
不,我告訴你迭代字典給你的鑰匙。如果你想迭代別的東西,你必須迭代別的東西。你沒有說你想要迭代什麼。 Python教程將是一個很好的地方,可以找出你可以迭代的內容,以及它會做什麼。 – 2010-04-28 23:42:48
不幸的是,要解釋所有可以從600個字符中的列表,字典和字符串中提取數據的方法有點困難,您可以在這些字符中添加註釋。我已經說過,你應該索引字典來獲得與某個密鑰相關的價值。我不確定你想要迭代什麼。瞭解內置的Python類型是下一步。 – 2010-04-28 23:52:06