투케이2K

57. (python/파이썬) [Mac Os] : [JSON] : import json 모듈 사용해 기본 jsonObject 생성 및 파싱 수행 본문

Python

57. (python/파이썬) [Mac Os] : [JSON] : import json 모듈 사용해 기본 jsonObject 생성 및 파싱 수행

투케이2K 2024. 5. 2. 20:53
반응형

[개발 환경 설정]

개발 툴 : VsCode

개발 언어 : python

 

[소스 코드]

# --------------------------------------------------------------
# [import]
# --------------------------------------------------------------
import json
# --------------------------------------------------------------



# --------------------------------------------------------------
# [요약 설명]
# --------------------------------------------------------------
# 1. import json : 파이썬에서 json 을 다룰 때 사용할 수 있습니다
# 2. 딕셔너리 : key , value 형태 데이터를 저장할 수 있습니다
# --------------------------------------------------------------



# --------------------------------------------------------------
# [class start]
# --------------------------------------------------------------



# --------------------------------------------------------------
# [main start]
# --------------------------------------------------------------


# [딕셔너리 생성]
dictionary = {"name":"투케이", "age":28, "sex":"M"}


# [dictionary to json 문자열 변환]
jsonDumps = json.dumps(dictionary)


# [json 데이터 파싱 수행]
jsonData = json.loads(jsonDumps)

name = jsonData["name"]
age = jsonData["age"]
sex = jsonData["sex"]


# [로그 출력]
print("")
print("----------------------------------------")
print("jsonDumps :: ", jsonDumps)
print("----------------------------------------")
print("name :: ", name)
print("----------------------------------------")
print("age :: ", age)
print("----------------------------------------")
print("sex :: ", sex)
print("----------------------------------------")
print("")


# --------------------------------------------------------------
# [main end]
# --------------------------------------------------------------



# --------------------------------------------------------------
# [class end]
# --------------------------------------------------------------
 

[결과 출력]


반응형
Comments