python把字典转换成json字符串

为什么80%的码农都做不了架构师?>>>   hot3.png

在Python标准库的json包中,提供了JSONEncoder和JSONDecoder两个类来实现Json字符串和dict类型数据的互相转换。

1.将Python dict类型转换成标准Json字符串
s={'a':'b','c':21}
d=json.JSONEncoder().encode(s)
print(type(d)) #str
print(d)       #{"a":"b","c":21}


2.将json字符串转换成Python dict类型
  
json_str='{"a":"b","c":21}'
   d=json.JSONDecoder().decode(json_str)
   print(type(d))  #dict
   print(d)        #{'a':'b','c':21}

转载于:https://my.oschina.net/jk409/blog/686523

你可能感兴趣的:(python把字典转换成json字符串)