2017-12-17 1 views
0

저는 거의 10 년 전 주위를 어지럽히는 것 외에 파이썬에 대한 사전 경험이 없습니다. OMDB API에서 데이터를 읽고 Python 2.7.14에서 오류를 계속 가져 오려고합니다.JSON을 덤프하는 동안 오류가 발생했습니다.

> {"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/MV5BNzQzOTk3OTAtNDQ0Zi00[email protected]._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"} 

이 지금까지 내 파이썬 코드는 다음과 같습니다 :

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

나는이 결과에 얻을

반환해야하는지의 예입니다.

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 

내가 여기 실종 무엇 :

내가 나에게 오류를 제공하는 json.dumps (결과를) 실행하려고이 단계 이후 JSON을 가져온 후?

+1

응답 본문은 이미 * JSON 문자열 *입니다. 왜 .dumps가 필요합니까? 해당 JSON을 나타내는 Python 객체를 원할 경우 ** response.json(), **으로 설명하지만 ** 문자열을 원한다면 deserialising은 무의미합니다. 당신이 실제로 무엇을 성취하려고하는지 명확하지 않습니다. – jonrsharpe

답변

2

resultsstr이 아니며, requests.models.Response 개체입니다.

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 
+0

정말 고마워! 개별 JSON 필드에 어떻게 접근합니까? –

관련 문제