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

StackOverflow https://stackoverflow.com/questions/17008110

  •  31-05-2022
  •  | 
  •  

문제

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. For instance if you follow this:

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

you will see the JSON object I am trying to use. I need to fetch the book description which is contained in the description key. I am using json.load to load it after fetching using 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:

>>> 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. You can use either the get() function, or do what I have done. The reason .items works is because of the built-in function, .items(), which returns the structure of a dictionary.

다른 팁

Try using the following:

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

As a note, a.items() and a['items'] is no the same thing. a.items() gives you the key, value pairs in a list of tuples.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top