Python
Python JSON 사용하기
크레이지J
2018. 4. 10. 12:49
반응형
+JSON
기본으로 설치되어 있다.
import json
-JSON 인코딩
customer={ 'id':111, 'name':'james',
'history':[ {'date':'2015-01-01', 'item':'iphome'}, {'date':'2016-01-01', 'item':'android'}]
}
jsonString = json.dumps(customer) # JSON 스트링으로 변환
출력
print(jsonString)
보기 좋게 출력하려면 인코딩시 indent 추가
jsonString = json.dumps(customer, indent=4)
-JSON 디코딩
JSON 스트링을 Python 객체로
dict = json.loads(jsonString)
dict['name']
dict['history'][0]['date']
for h in dict['history']:
print(h['date'], h['item'])