通过 JSON 对象进行迭代

我尝试迭代一个 JSON 对象来导入数据,即 title 和 link。我似乎不能得到的内容是过去的 :

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

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

655469 次浏览

您对 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 的键。因为这就是迭代结果的结果。如果希望访问与该 dict 中的键相关联的值,可以使用例如 json_object[0][song]

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

在反序列化 JSON 之后,就有了一个 python 对象。

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

json_object[0].items()


json_object[0]["title"]

等等。

我会像这样解决这个问题

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

我相信你的意思是:

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

注意: 如果在 Python2中,您可以使用 song.iteritems而不是 song.items

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

对于 Python3,您必须解码从 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


如果不进行解码,在 Python3中将得到字节和字符串错误。

对于通过 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'])

如果可以将 json 字符串存储在变量 jsn_string

import json


jsn_list = json.loads(json.dumps(jsn_string))
for lis in jsn_list:
for key,val in lis.items():
print(key, val)

产出:

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);
http://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

添加另一个解决方案(Python3)——迭代目录中的 json 文件和每个文件中的所有对象并打印相关字段。

请参见代码中的注释。

import os,json


data_path = '/path/to/your/json/files'


# 1. Iterate over directory
directory = os.fsencode(data_path)
for file in os.listdir(directory):
filename = os.fsdecode(file)
# 2. Take only json files
if filename.endswith(".json"):
file_full_path=data_path+filename
# 3. Open json file
with open(file_full_path, encoding='utf-8', errors='ignore') as json_data:
data_in_file = json.load(json_data, strict=False)
# 4. Iterate over objects and print relevant fields
for json_object in data_in_file:
print("ttl: %s, desc: %s" % (json_object['title'],json_object['description']) )