如何从google books API python返回的这个JSON对象中访问一个值?

时间:2013-06-09 09:06:07

标签: python json google-books

我从python脚本访问Google Books API,我需要在我获得的JSON对象中获取特定值。例如,如果你这样做:

https://www.googleapis.com/books/v1/volumes?q=9781607055389

您将看到我正在尝试使用的JSON对象。我需要获取description键中包含的书籍描述。我在使用json.load获取后使用urllib2加载它。

我尝试过以下操作无济于事:

a = json.load(urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=9781607055389'))
print a.items[0].description

2 个答案:

答案 0 :(得分:2)

description在另一个词典中,你有这个词:

>>> print a['items'][0]['volumeInfo']['description']
Photo tutorials show stitching in action for 50+ free-motion quilting designs to create modern quilts with classic style! Popular blogger and designer, Natalia Bonner, illustrates her instructions with detailed photos that make it easier to get beautiful results on your home sewing machine. Learn how to quilt all-over, as filler, on borders, and on individual blocks...using loops and swirls, feathers and flames, flowers and vines, pebbles and more! Includes tips for choosing batting and thread, layering and basting, starting and stopping, and prepping your machine are included. After you've practiced, show off your new skills with six geometric quilt projects.

您访问字典值的意图不是您在python中所做的。您可以使用get()函数,也可以执行我已完成的操作。 .items起作用的原因是内置函数.items(),它返回字典的结构。

答案 1 :(得分:0)

尝试使用以下内容:

a['items'][0]['volumeInfo']['description']

作为一个注释,a.items()和['items']不是一回事。 a.items()为您提供元组列表中的键值对。