当前第2页 返回上一页
2、json.dump()和json.load()主要用来读写json文件函数
二、测试
1 2 3 4 5 6 7 | import json
# json.dumps()函数的使用,将字典转化为字符串
dict1 = { "age" : "12" }
json_info = json.dumps(dict1)
print ( "dict1的类型:" +str(type(dict1)))
print ( "通过json.dumps()函数处理:" )
print ( "json_info的类型:" +str(type(json_info)))
|
1 2 3 4 5 6 7 8 | 1 import json
2
3 # json.loads函数的使用,将字符串转化为字典
4 json_info = '{"age": "12"}'
5 dict1 = json.loads(json_info)
6 print ( "json_info的类型:" +str(type(json_info)))
7 print ( "通过json.dumps()函数处理:" )
8 print ( "dict1的类型:" +str(type(dict1)))
|
1 2 3 4 5 6 | 1 import json
2
3 # json.dump()函数的使用,将json信息写进文件
4 json_info = "{'age': '12'}"
5 file = open( '1.json' , 'w' ,encoding= 'utf-8' )
6 json.dump(json_info,file)
|
1 2 3 4 5 6 | 1 import json
2
3 # json.load()函数的使用,将读取json信息
4 file = open( '1.json' , 'r' ,encoding= 'utf-8' )
5 info = json.load(file)
6 print (info)
|
以上就是认识python的json.dumps()和json.loads()的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python实现简单淘宝秒杀功能
Python中找出numpy array数组的最值及其索引方法
嵌入式与Python选哪个
Python如何用sum函数求和
在cmd中Python如何卸载模块
Python中add函数怎么用
Python中paramiko模块实现远程控制以及传输的示例
Python的数据结构
Python中del函数的用法详解
Python实现按当前日期(年、月、日)创建多级目录的方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 认识python的json.dumps()和json.loads()