【问题标题】:How to access a value from this JSON object returned from the google books API python?如何访问从谷歌图书 API python 返回的这个 JSON 对象的值?
【发布时间】:2013-06-09 09:06:06
【问题描述】:

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

【问题讨论】:

    标签: python json google-books


    【解决方案1】:

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

    【讨论】:

    • 感谢您为我解决这个问题。我对为什么 .items() 起作用感到困惑。
    【解决方案2】:

    尝试使用以下方法:

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

    请注意,a.items() 和 a['items'] 不是一回事。 a.items() 为您提供元组列表中的键值对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多