python把字典转换成json字符串
2016-06-03 11:46
459 查看
在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}
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}
相关文章推荐
- python-module 'pymysql' has no attribute 'connect'
- 解决在Windows server 2008 安装Python2.7版本
- numpy常用函数
- Python configparser模块
- python urllib2
- python爬虫基础A
- python中的@符号的作用
- python 基本语法
- Python NLTK学习
- PYTHON 自定义异常类的使用
- Python的静态方法和类方法有什么区别
- Python 输出文字带颜色
- 命令形式新建第一个python 项目
- Python函数式练习
- 小强IT游记之大连行
- adaboost算法
- 小强IT游记之厦门行
- Python常用模块之sys
- python内置函数每个执行一次
- python 2与3的区别汇总