[英]How to access a value from this JSON object returned from the google books API python?
I am accessing the Google Books API from a python script and I need to fetch a particular value in the JSON object I get. 我正在从python脚本访问Google Books API,我需要在获取的JSON对象中获取特定值。 For instance if you follow this:
例如,如果您遵循以下步骤:
https://www.googleapis.com/books/v1/volumes?q=9781607055389 https://www.googleapis.com/books/v1/volumes?q=9781607055389
you will see the JSON object I am trying to use. 您将看到我尝试使用的JSON对象。 I need to fetch the book description which is contained in the
description
key. 我需要获取
description
密钥中包含的书籍描述。 I am using json.load
to load it after fetching using urllib2
. 我正在使用
json.load
在使用urllib2
提取后加载它。
I have tried doing the following to no avail: 我尝试执行以下操作无济于事:
a = json.load(urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=9781607055389'))
print a.items[0].description
description
is in another dictionary, which you have forgotton: 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.
Your intention of accessing dictionary values is not what you do in python. 您访问字典值的意图不是您在python中所做的。 You can use either the
get()
function, or do what I have done. 您可以使用
get()
函数或执行我所做的事情。 The reason .items
works is because of the built-in function, .items()
, which returns the structure of a dictionary. .items
起作用的原因是由于内置函数.items()
可以返回字典的结构。
Try using the following: 尝试使用以下内容:
a['items'][0]['volumeInfo']['description']
As a note, a.items() and a['items'] is no the same thing. 需要注意的是,a.items()和a ['items']并不相同。 a.items() gives you the key, value pairs in a list of tuples.
a.items()在元组列表中为您提供键,值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.