gpt4 book ai didi

python - 遍历 JSON 对象

转载 作者:IT老高 更新时间:2023-10-28 21:11:41 31 4
gpt4 key购买 nike

我正在尝试遍历 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

此代码仅打印 : 之前的信息。(忽略 Justin Bieber 的轨道 :))

最佳答案

我相信你的意思可能是:

from __future__ import print_function

for song in json_object:
# now song is a dictionary
for attribute, value in song.items():
print(attribute, value) # example usage

注意:如果在 Python 2 中,您可以使用 song.iteritems 代替 song.items

关于python - 遍历 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733813/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com