簡體   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