json 모듈 사용시 not JSON serializable 에러 > PYTHON 프로그래밍

본문 바로가기

PYTHON 프로그래밍

PYTHON 프로그래밍

8. json 모듈 사용시 not JSON serializable 에러

페이지 정보

작성자 관리자 댓글 0건 조회 5,900회 작성일 20-07-20 00:07

본문

8. json 모듈 사용시 not JSON serializable 에러

가장 대표적인 예가 바로 python 날짜/시간 타입을 곧이 곧대로 넣었을 경우이다.



import datetime, json

today = datetime.date.today()

data = { 'date': today}

json_data = json.dumps(data) # TYPE ERROR :: not JSON serializable


today 라는 날짜 변수를 json 모듈이 어찌해야 할 지 모르는 경우이다. 

물론 다음과 같이 날짜 변수를 문자열로 잘 바꿔주면 문제는 없을 것이다.



import datetime, json

today = datetime.date.today()

data = { 'date': today.strftime('%Y-%m-%d')}

json_data = json.dumps(data)



Copyright © LEELAB.CO.KR. All rights reserved.