兩種方法:str()以及json.dumps()
注意:單引號雙引號的區別
str方法將其變為單引號,json.dumps方法仍是雙引號!!!!
- 初始字典為雙引號
import json d={"name":"lisa","gender":"male"} print(type(d)) str_d=str(d) print("str_d:",str_d) print("str_d的類型",type(str_d)) json_d=json.dumps(d) print("json_d:",json_d) print("json_d的類型",type(json_d)) #################### <class 'dict'> str_d: {'name': 'lisa', 'gender': 'male'} str_d的類型 <class 'str'> json_d: {"name": "lisa", "gender": "male"} json_d的類型 <class 'str'>
- 初始字典為單引號
d={'name':'lisa','gender':'male'} print(type(d)) str_d=str(d) print("str_d:",str_d) print("str_d的類型",type(str_d)) json_d=json.dumps(d) print("json_d:",json_d) print("json_d的類型",type(json_d)) ###################### <class 'dict'> str_d: {'name': 'lisa', 'gender': 'male'} str_d的類型 <class 'str'> json_d: {"name": "lisa", "gender": "male"} json_d的類型 <class 'str'>