遍历JSON对象


109

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

此代码仅在之前打印信息:。(忽略贾斯汀·比伯的曲目:)

Answers:


78

您加载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]:,您将遍历字典的键。因为那是您遍历dict时得到的。如果要访问与该字典中的键关联的值,则可以使用json_object[0][song]

这些都不是特定于JSON的。这只是基本的Python类型,其基本操作如本教程所述。


我不明白。我试图遍历您所说的话。我很确定这是一个关于json的问题
myusuf3 2010年

7
否。我告诉您,对dict进行迭代可以为您提供密钥。如果要迭代其他内容,则必须迭代其他内容。您没有说要迭代的内容。Python教程将是找到可以迭代的内容以及它将完成的工作的好地方。
Thomas Wouters 2010年

5
不幸的是,要解释从列表,字典和字符串中提取的600个字符中提取数据的所有方式有点困难。我已经说过,您应该索引字典以获取与键关联的值。我不确定您要迭代什么。了解内置Python类型是下一步。
Thomas Wouters 2010年

当您要获取单个项目时,不需要太多迭代。也许您要迭代的json_object不是json_object[0],然后从每个字典中获取单个项目。
Thomas Wouters'2010-04-29

101

我相信您可能是说:

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中使用if song.iteritems代替song.items


对于属性,song.iteritems()中的值:此行中的逗号表示什么?
zakdances 2012年

for (attribute, value) in song.iteritems():(var1, var2) = (1, 2)或相同var1, var2 = 1, 2dict.iteritems()产生(key, value)对(元组)。搜索“ python元组解压缩”。
tzot 2012年

1
对于python 3,更改song.iteritemssong.items
大南瓜'18

44

这个问题已经存在很长时间了,但是我想贡献我通常如何遍历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()

2
上面的代码中没有字符串下标;jsonObject是一个dict。在上面的代码中,我希望使用for key, value in jsonObject.items():
tzot

22

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

在这种情况下,您有一个由字典组成的列表:

json_object[0].items()

json_object[0]["title"]

等等


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

3

通过JSON进行迭代,您可以使用以下代码:

json_object = json.loads(json_file)
for element in json_object: 
    for value in json_object['Name_OF_YOUR_KEY/ELEMENT']:
        print(json_object['Name_OF_YOUR_KEY/ELEMENT']['INDEX_OF_VALUE']['VALUE'])

2

对于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

如果不解码,Python 3中将得到字节与字符串错误。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.