繁体   English   中英

如何从Google图书API python返回的JSON对象访问值?

[英]How to access a value from this JSON object returned from the google books API python?

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

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

尝试使用以下内容:

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

需要注意的是,a.items()和a ['items']并不相同。 a.items()在元组列表中为您提供键,值对。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM