import json
def str_to_dict():
res = json.loads('{"name": "xiaoming", "age": 18}')
print("对象类型:", type(res))
print("姓名:{}, 年龄:{}".format(res['name'], res['age']))
def dict_to_str():
dictObj = {"name": "xiaofang", "age": 25}
dictStr = json.dumps(dictObj)
print("对象类型:", type(dictStr))
print(dictStr)
if __name__ == '__main__':
str_to_dict()
dict_to_str()
输出结果:
对象类型: <class 'dict'>
姓名:xiaoming, 年龄:18
对象类型: <class 'str'>
{"name": "xiaofang", "age": 25}