Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python - 문자열을 json 객체로 변환하는 방법

<시간/>

데이터는 일반적으로 사전 형식으로 변환하고 추가 작업에 사용하는 데 필요한 의미 있는 정보를 추출하기 위해 해당 데이터를 사용하기 위해 많은 웹 API에서 사전(JSON 개체) 형식의 문자열로 전송 및 가져옵니다.

예시

# converting string to json
# using json.loads
import json
# inititialising json object
ini_string = {'vishesh': 1, 'ram' : 5, 'prashant' : 10, 'vishal' : 15}
# printing initial json
ini_string = json.dumps(ini_string)
print ("initial 1st dictionary", ini_string)
print ("type of ini_object", type(ini_string))
# converting string to json
final_dictionary = json.loads(ini_string)
# printing final result
print ("final dictionary", str(final_dictionary))
print ("type of final_dictionary", type(final_dictionary))

출력

('initial 1st dictionary', '{"vishal": 15, "ram": 5, "vishesh": 1, "prashant": 10}')
('type of ini_object', <type 'str'>)
('final dictionary', "{u'vishal': 15, u'ram': 5, u'vishesh': 1, u'prashant': 10}")
('type of final_dictionary', <type 'dict'>)