- 利用eval可以将字典格式的字符串与转为字典
mstr = '{"name":"www.coolcou.com","age":10}'
print(eval(mstr), type( eval(mstr) ))
输出结果:
{'name': 'www.coolcou.com', 'age': 10} <class 'dict'>
- JSON到字典转化:
import json
mstr = '{"name":"www.coolcou.com","age":10}'
dictinfo = json.loads(mstr)
print(dictinfo["name"])
print(dictinfo["age"])
输出:
www.coolcou.com
10
字典到字符串转化:
import json
mstr = '{"name":"www.coolcou.com","age":10}'
dictinfo = json.loads(mstr)
jsoninfo = json.dumps(dictinfo)
print(jsoninfo)
输出:
{"name": "www.coolcou.com", "age": 10}
评论前必须登录!
注册