Python JSON 다루기

Jmnote (토론 | 기여)님의 2020년 1월 12일 (일) 14:07 판 (→‎개요)

1 개요

Python JSON 다루기
파이썬 JSON 처리
JSON 문자열을 파이썬 딕셔너리로 변환
import json
x = '{"name":"홍길동", "age":18}'
y = json.loads(x)
print(type(y))
# <class 'dict'>
print(y)
# {'name': '홍길동', 'age': 18}
print(y['age'])
# 18
파이썬 딕셔너리를 JSON 문자열로 변환
import json
x = {"name":"홍길동", "age":18}
y = json.dumps(x)
print(type(y))
# <class 'str'>
print(y)
# {"name": "\ud64d\uae38\ub3d9", "age": 18}
→ 원래 JSON 형식은 멀티바이트 유니코드 문자를 \uxxxx로 표현하는 것이 표준이다.
JSON 문자열을 파이썬 딕셔너리로 변환 2
import json
x = '{"name": "\ud64d\uae38\ub3d9", "age": 18}'
y = json.loads(x)
print(type(y))
# <class 'dict'>
print(y)
# {'name': '홍길동', 'age': 18}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}