数据通常在许多Web 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'>)