1

我正在从 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
4

2 回答 2

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(),它返回字典的结构。

于 2013-06-09T09:12:30.687 回答
0

尝试使用以下内容:

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

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

于 2013-06-09T09:12:39.710 回答