Python을 배워보자

ㅎㅎ

_Blue_Sky_ 2025. 3. 6. 09:16
728x90

import json

data = {
    "이름": "홍길동",
    "나이": 30,
    "거주지": "서울",
    "취미": ["독서", "여행", "게임"]
}

# 딕셔너리를 JSON 문자열로 변환
json_string = json.dumps(data, ensure_ascii=False, indent=4)
print(json_string)

# JSON 문자열을 딕셔너리로 변환
loaded_data = json.loads(json_string)
print(loaded_data)

# 딕셔너리를 JSON 파일로 저장
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

# JSON 파일에서 딕셔너리로 불러오기
with open("data.json", "r", encoding="utf-8") as f:
    loaded_data_from_file = json.load(f)
    print(loaded_data_from_file)

728x90