我从表里读取了json类型的字段,得到的是字符串。如下格式
'{"firstname":"David","surname":"John","age":32, "hobbies": [0, 2, 7]}'
这个就是文本,无法直接方便得得到各个key的结果。怎么直接把文本变成python中字典dict类型的呢?
4个回答
eval 也行,但建议用 json.loads
import json
json_str = '{"firstname":"David","surname":"John","age":32, "hobbies": [0, 2, 7]}'
json_obj = json.loads(json_str)
my_texts = '{"firstname":"David","surname":"John","age":32, "hobbies": [0, 2, 7]}'
my_dict = eval(my_texts)
eval是python内自带的函数
SofaSofa数据科学社区DS面试题库 DS面经
谢谢!
-
zzzz
2020-07-05 23:04