简体   繁体   English

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

[英]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.

相关问题 通过Python中的值搜索从API返回的JSON - Search JSON returned from API by value in Python 如何使用Python从Google Translate API访问JSON转换 - How to access JSON translation from Google Translate API with Python 无法序列化从python中的rest api返回的json数组对象 - Unable to serialize json array object returned from rest api in python 如何从 Google 图书 api 中获取作者姓名? - How to get the authors name from Google books api? 如何从 Python 中的 object 返回的字典中访问特定值 - How can I access a specific value from the dictionary returned by this object in Python 在使用requests.get调用API后,如何从返回的JSON对象替换键的值? - How do I replace a value of a key from a returned JSON object after calling an API using requests.get? 通过Google API从返回的json中提取纬度/经度(地理编码) - Extracting Lat/Long from returned json via google api(Geocoding) Google图书API-如何将Book与Book Edition分开? - Google Books API - How do I separate Book from Book Edition? 从Google Analytics(分析)python API返回的“错误”配置文件ID - 'Wrong' profile ID being returned from Google Analytics python API 'method' 对象在尝试 google api 书籍时不可下标 - 'method' object is not subscriptable trying for google api books
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM