파이썬에서 json 인덱싱에 어려움을 겪고있습니다.. 초보적인 문제같은데

조회수 1230회
response = requests.get( url, headers=headers)
match=response.json()
print(match)
print("_____")
print(type(match))
print("_____")
match =json.dumps(match)
print(type(match))
print("_____")

match = json.loads(str(match))
print(type(match))
print("_____")
print(match)
print("_____")

print(match["matches"]["matches"])
print("_____")


이 코드를 실행하면

{'matches': [{'matchType': 'free', 'matches': [{'accountNo': '636755761583559872', 'matchId': 'B7jP2vwWNK', 'matchType': 'free', 'teamId': 'north', 'characterName': 'Medusa', 'matchResult': 'lose', 'seasonType': '0', 'startTime': '2019-02-19T05:49:47', 'endTime': '2019-02-19T06:10:55'}]}]}
_____
<class 'dict'>
_____
<class 'str'>
_____
<class 'dict'>
_____
{'matches': [{'matchType': 'free', 'matches': [{'accountNo': '636755761583559872', 'matchId': 'B7jP2vwWNK', 'matchType': 'free', 'teamId': 'north', 'characterName': 'Medusa', 'matchResult': 'lose', 'seasonType': '0', 'startTime': '2019-02-19T05:49:47', 'endTime': '2019-02-19T06:10:55'}]}]}
_____

이렇게 출력이 되고 마지막에 인덱싱에서 에러가납니다..

   print(match["matches"]["matches"])
TypeError: list indices must be integers or slices, not str
  • 만약 그게 문제라면 match["matches"][0]["matches"][0] 하시면 될듯 한데? https://www.codeit.kr/questions/7 엽토군 2019.2.19 15:51
  • 오... 중간중간 [0]쓰고 마지막에만 빼니까 정상출력 되네요 감사합니다 Duk Gi Awa Yang 2019.2.19 15:52
  • 위에서 설명 해주셨듯이 Dict 타입과 List 타입이 혼용되어 있어서 그렇습니다. Dict 은 Key:Value , List 는 array로 보면됩니다. {} 는 Dict , [] 는 List 띠리사 match["matches"][0]["matches"] 을 하셔야 원하시는 값을 얻을수 있습니다. ㅎㅎ 알 수 없는 사용자 2019.11.12 13:59

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)