python - 如何访问从 google books API python 返回的 JSON 对象的值?

标签 python json google-books

我正在从 python 脚本访问 Google Books API,并且需要在获得的 JSON 对象中获取特定值。例如,如果您遵循以下步骤:

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

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

我已尝试执行以下操作但无济于事:

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

最佳答案

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

关于python - 如何访问从 google books API python 返回的 JSON 对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008110/

相关文章:

java - Googleapis.com 未知主机异常

python - 在 Linux 中获取进程的最大 CPU/RAM 使用率

python - 如何导入使用 --user 选项安装的模块?

java - 命令输出解析

java - java - 如何在Java中使用Jackson Annotation按原样反序列化时间戳?

javascript - 如何使用getJson将变量发送到php文件

javascript - 谷歌图书 API : Cannot read property 'thumbnail' of undefined

javascript - 将 ajax 响应属性放入 Google 图书封面图像的变量中

python - 如何在不使用python指定id的情况下在elasticsearch中添加doc

php - amazon elastic beanstalk eb 本地运行在我的机器中出现问题