赞
踩
- import json
-
- data1 = {
- 'no' : 1,
- 'name' : 'Python',
- 'url' : 'https://www.python.org/'
- }
-
- # Python 字典类型转换为 JSON 对象
- json_str = json.dumps(data1)
- print ("Python 原始数据:", repr(data1))
- print ("JSON 对象:", json_str)
-
- # 将 JSON 对象转换为 Python 字典
- data2 = json.loads(json_str)
- print ("data2['name']: ", data2['name'])
- print ("data2['url']: ", data2['url'])
-
-
- # 写入 JSON 数据
- with open('data.json', 'w') as f:
- json.dump(data, f)
-
- # 读取数据
- with open('data.json', 'r') as f:
- data = json.load(f)
Python | JSON |
---|---|
dict | object |
list, tuple |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。