2010-04-28 182 views
50

我想遍历一个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 

此代码只打印:之前的信息。 (忽略贾斯汀比伯轨道 :))

回答

49

您加载的JSON数据有点脆弱。相反的:

json_raw= raw.readlines() 
json_object = json.loads(json_raw[0]) 

你真的应该只是做:

json_object = json.load(raw) 

你不要以为你所得到的“JSON对象”。你有什么是一个列表。该列表包含两个字典。字典包含各种键/值对,所有字符串。当你做json_object[0]时,你要求列表中的第一个字典。当你迭代它时,用for song in json_object[0]:,你迭代字典的键。因为这是你迭代字典时得到的。如果您想访问该字典中与该键相关的值,则可以使用例如json_object[0][song]

这些都不是特定于JSON。它只是基本的Python类型,其基本操作在任何教程中都有介绍。

+0

我不明白这一点。我试着重复你说的出界。我很确定它的一个问题关于json – myusuf3 2010-04-28 23:41:08

+7

不,我告诉你迭代字典给你的钥匙。如果你想迭代别的东西,你必须迭代别的东西。你没有说你想要迭代什么。 Python教程将是一个很好的地方,可以找出你可以迭代的内容,以及它会做什么。 – 2010-04-28 23:42:48

+5

不幸的是,要解释所有可以从600个字符中的列表,字典和字符串中提取数据的方法有点困难,您可以在这些字符中添加注释。我已经说过,你应该索引字典来获得与某个密钥相关的价值。我不确定你想要迭代什么。了解内置的Python类型是下一步。 – 2010-04-28 23:52:06

21

反序列化JSON之后,您将拥有一个python对象。使用常规的对象方法。

在这种情况下,你有一个列表做出词典:

+0

感谢您的帮助! :) – myusuf3 2010-04-28 23:49:33

8

我会更多的解决这个问题,这样

import json 
import urllib2 

def last_song(user, limit): 
    # Assembling strings with "foo" + str(bar) + "baz" + ... generally isn't 
    # as nice as using real string formatting. It can seem simpler at first, 
    # but leaves you less happy in the long run. 
    url = 'http://gsuser.com/lastSong/%s/%d/' % (user, limit) 

    # urllib.urlopen is deprecated in favour of urllib2.urlopen 
    site = urllib2.urlopen(url) 

    # The json module has a function load for loading from file-like objects, 
    # like the one you get from `urllib2.urlopen`. You don't need to turn 
    # your data into a string and use loads and you definitely don't need to 
    # use readlines or readline (there is seldom if ever reason to use a 
    # file-like object's readline(s) methods.) 
    songs = json.load(site) 

    # I don't know why "lastSong" stuff returns something like this, but 
    # your json thing was a JSON array of two JSON objects. This will 
    # deserialise as a list of two dicts, with each item representing 
    # each of those two songs. 
    # 
    # Since each of the songs is represented by a dict, it will iterate 
    # over its keys (like any other Python dict). 
    baby, feel_good = songs 

    # Rather than printing in a function, it's usually better to 
    # return the string then let the caller do whatever with it. 
    # You said you wanted to make the output pretty but you didn't 
    # mention *how*, so here's an example of a prettyish representation 
    # from the song information given. 
    return "%(SongName)s by %(ArtistName)s - listen at %(link)s" % baby 
45

json_object[0].items() 

json_object[0]["title"] 

等。我相信你可能是指:

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

属性,在song.iteritems()中的值:这一行中的逗号意味着什么? – zakdances 2012-08-09 23:55:34

+0

与song.iteritems():或var(var2)=(1,2)或var1,var2 = 1,2的'(属性,值)相同。 'dict.iteritems()'产生'(键,值)'对(元组)。搜索“python tuple unpacking”。 – tzot 2012-08-11 12:47:53

22

这个问题在这里已经很长时间了,但我想贡献一下我通常如何遍历JSON对象。在下面的示例中,我展示了一个包含JSON的硬编码字符串,但JSON字符串可以很容易地来自Web服务或文件。

import json 

def main(): 

    # create a simple JSON array 
    jsonString = '{"key1":"value1","key2":"value2","key3":"value3"}' 

    # change the JSON string into a JSON object 
    jsonObject = json.loads(jsonString) 

    # print the keys and values 
    for key in jsonObject: 
     value = jsonObject[key] 
     print("The key and value are ({}) = ({})".format(key, value)) 

    pass 

if __name__ == '__main__': 
    main() 
+0

字符串索引必须是整数,而不是str – 2016-05-27 21:55:32

1

对于Python 3,您必须解码从Web服务器返回的数据。比如我对数据进行解码的UTF8然后用它处理:

# example of json data object group with two values of key id 
jsonstufftest = '{'group':{'id':'2','id':'3'}} 
# always set your headers 
headers = {'User-Agent': 'Moz & Woz'} 
# the url you are trying to load and get json from 
url = 'http://www.cooljson.com/cooljson.json' 
# in python 3 you can build the request using request.Request 
req = urllib.request.Request(url,None,headers) 
# try to connect or fail gracefully 
try: 
    response = urllib.request.urlopen(req) # new python 3 code -jc 
except: 
    exit('could not load page, check connection') 
# read the response and DECODE 
html=response.read().decode('utf8') # new python3 code 
# now convert the decoded string into real JSON 
loadedjson = json.loads(html) 
# print to make sure it worked 
print (loadedjson) # works like a charm 
# iterate through each key value 
for testdata in loadedjson['group']: 
    print (accesscount['id']) # should print 2 then 3 if using test json 

如果不进行解码你会得到字节串VS错误在Python 3