#json.dumps(dict,indent)
#json.dump(dict, file_pointer)
#json으로 형변환
d = {"group1":[
{'name':'Park', 'age':'32', 'sex':'Male'},
{'name':'Cho', 'age':'44', 'sex':'Female'},
{'name':'Kang', 'age':'39', 'sex':'Female', 'married':'No'}
],
"group2":[
{'name':'Kim', 'age':'23', 'sex':'Male', 'married':'Yes'},
{'name':'Lee', 'age':'37', 'sex':'Male', 'married':'No'}
],
"type": {'a':'employee', 'b':'office', 'c':'director', 'd':'manager', 'e':'service provider'}
}
#json 형식으로 형변환하시오
#방법1
import json #json라이브러리 호출
json_obj = json.dumps(d,indent=3) #json.dumps 함수 사용
print(json_obj)
#방법2 특정파일에 작성
with open("C:/Users/User/Downloads/python_basic_1.5/2.QnA/source/32-2.json") as out:
json.dump(d, out)
#dict으로 형변환
d = '''
{"group1":[
{'name': 'Park', 'age': '32', 'sex': 'Male'},
{'name': 'Cho', 'age': '44', 'sex': 'Female'},
{'name': 'Kang', 'age': '39', 'sex': 'Female', 'married': 'No'}
],
"group2":[
{'name': 'Kim', 'age': '23', 'sex': 'Male', 'married': 'Yes'},
{'name': 'Lee', 'age': '37', 'sex': 'Male', 'married': 'No'}
],
"type" : {"a": "employee", "b": "officer", "c": "director", "d": "manager", "e": "service provider"}
}
'''
#json 형식으로 형변환하시오
import json
# 방법1
result1 = json.loads(d.replace("'", "\""))
# 출력
print(result1)
# 확인
print(type(result1))
print()
# 방법2
with open("C:/Users/User/Downloads/python_basic_1.5/2.QnA/source/33-1.json", "r") as out:
result2 = json.load(out)
# 출력
print(result2)
# 확인
print(type(result2))