在python中获取JSON字段

时间:2015-12-08 16:08:44

标签: python json

我是python的新手。

我有一个像这样的JSON响应数组(来自theMovieDB的API),这是数组响应[0]的第一个成员:

{
page: 1,
results: [
{
poster_path: "/yVHNGrsIj3FNDg9lgseiZMlHbjJ.jpg",
adult: false,
overview: "The film centers on three brothers who, upon learning they only  have a few days left to live, set off to reverse a lifetime of mistakes. Hopper and Simmons are playing the brothers' father and uncle, respectively, while Caan is one of the brothers. Helfer is Caan's girlfriend, a woman with a dangerous past.",
release_date: "2010-09-17",
genre_ids: [
   35,
   18
 ],
id: 88057,
original_title: "A Beginner's Guide to Endings",
original_language: "en",
title: "A Beginner's Guide to Endings",
backdrop_path: "/dDYli8oDYOkgRp5f0kMqgQ1TSmt.jpg",
popularity: 1.006656,
vote_count: 4,
video: false,
vote_average: 5.75
}
],
total_results: 1,
total_pages: 1
}

我想抓住id字段,所以我尝试了响应[0] ['结果'] ['id'] - 但它不起作用! (响应[0] ['结果']确实有效!) 任何想法为什么??

2 个答案:

答案 0 :(得分:4)

results键引用词典的列表;你必须使用整数来编制索引:

responds[0]['results'][0]['id']

为您提供该列表中第一个字典的id键。

答案 1 :(得分:0)

Martijn responds[0]['results']提到的是一个列表,responds[0]['results'][0]是一个字典。您可以在here下面看到json数组的可视化表示。
为了访问id,您必须使用:response['results'][0]['id']