Python 尝试转储JSON时出错

Python 尝试转储JSON时出错,python,json,python-2.7,Python,Json,Python 2.7,除了近10年前的一些胡闹之外,我以前没有Python方面的经验。我试图从OMDB API读取数据,但在Python 2.7.14中不断出现错误 以下是应返回内容的示例: > {"Title":"The Matrix","Year":"1999","Rated":"R","Released":"31 Mar > 1999","Runtime":"136 min","Genre":"Action, Sci-Fi","Director":"Lana > Wachowski, Lill

除了近10年前的一些胡闹之外,我以前没有Python方面的经验。我试图从OMDB API读取数据,但在Python 2.7.14中不断出现错误

以下是应返回内容的示例:

> {"Title":"The Matrix","Year":"1999","Rated":"R","Released":"31 Mar
> 1999","Runtime":"136 min","Genre":"Action, Sci-Fi","Director":"Lana
> Wachowski, Lilly Wachowski","Writer":"Lilly Wachowski, Lana
> Wachowski","Actors":"Keanu Reeves, Laurence Fishburne, Carrie-Anne
> Moss, Hugo Weaving","Plot":"A computer hacker learns from mysterious
> rebels about the true nature of his reality and his role in the war
> against its
> controllers.","Language":"English","Country":"USA","Awards":"Won 4
> Oscars. Another 34 wins & 45
> nominations.","Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BNzQzOTk3OTAtNDQ0Zi00ZTVkLWI0MTEtMDllZjNkYzNjNTc4L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg","Ratings":[{"Source":"Internet
> Movie Database","Value":"8.7/10"},{"Source":"Rotten
> Tomatoes","Value":"87%"},{"Source":"Metacritic","Value":"73/100"}],"Metascore":"73","imdbRating":"8.7","imdbVotes":"1,354,586","imdbID":"tt0133093","Type":"movie","DVD":"21
> Sep 1999","BoxOffice":"N/A","Production":"Warner Bros.
> Pictures","Website":"http://www.whatisthematrix.com","Response":"True"}
这是迄今为止我的Python代码:

import requests
API_KEY = '******'
Movie = 'The Matrix'
results = requests.get("http://www.omdbapi.com/", 
              params={'apikey': API_KEY, 't': Movie})
我的成绩很好

在此阶段之后导入json后,我尝试运行json.dumps(results),这会导致错误:

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    json.dumps(results)
  File "C:\Python27\lib\json\__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "C:\Python27\lib\json\encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Response [200]> is not JSON serializable
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
json.dumps(结果)
文件“C:\Python27\lib\json\\uuuu init\uuuu.py”,第244行,转储
返回默认编码器编码(obj)
文件“C:\Python27\lib\json\encoder.py”,第207行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
文件“C:\Python27\lib\json\encoder.py”,第270行,在iterencode中
返回_iterencode(o,0)
默认情况下,文件“C:\Python27\lib\json\encoder.py”,第184行
raise TypeError(repr(o)+“不可JSON序列化”)
TypeError:不可序列化JSON

我在这里遗漏了什么?

results
不是一个
str
,而是一个
requests.models.Response
对象。尝试调用它的
.json()
方法,如下所示:

import requests
API_KEY = '******'
Movie = 'The Matrix'
results = requests.get("http://httpbin.org/get", 
              params={'apikey': API_KEY, 't': Movie})

data = results.json()

print data

results
不是一个
str
,它是一个
requests.models.Response
对象。尝试调用它的
.json()
方法,如下所示:

import requests
API_KEY = '******'
Movie = 'The Matrix'
results = requests.get("http://httpbin.org/get", 
              params={'apikey': API_KEY, 't': Movie})

data = results.json()

print data

响应的主体已经是一个JSON字符串,为什么需要.dumps呢?如果您想要表示该JSON的Python对象,它是
response.JSON()
,如文档所述,但是如果您想要字符串,那么反序列化它是没有意义的。现在还不清楚您实际想要实现什么。响应的主体已经是一个JSON字符串,为什么需要.dumps呢?如果您想要表示该JSON的Python对象,它是
response.JSON()
,如文档所述,但是如果您想要字符串,那么反序列化它是没有意义的。现在还不清楚你到底想要实现什么。非常感谢!我将如何继续访问各个JSON字段?非常感谢!我将如何继续访问各个JSON字段?