
本文共 3126 字,大约阅读时间需要 10 分钟。
Python JSON ���������������
JSON (JavaScript Object Notation) ������������������������������������������������ ECMAScript ���������������������������������������������������������������������������������������������������������������������
Python JSON ���������������
��� Python 3.0 ���������������������json
������������������������������������json.dumps()
��� json.loads()
������������������ JSON ��������������������������������������������������������������������� Python ������������������������ JSON ��������������� JSON ������������������������ Python ���������������
Python ��������������� JSON ���������������������
��� JSON ���������������������Python ��������������������������� JSON ���������������������������������������������������������������������������
Python ������ | JSON ������ |
---|---|
dict | object |
list, tuple | array |
str | string |
int, float | number |
bool (True, False) | boolean |
None | null |
JSON ��������� Python ���������������������
JSON ������ | Python ������ |
---|---|
object | dict |
array | list |
string | str |
number | int |
boolean | bool |
null | None |
json.dumps()���json.loads()���������������
������������������
������������������������ Python ��������������������� JSON ������������������
import json# ���������������������������������������������data = { 'name': 'Codercto', 'url': 'http://www.codercto.com', 'anguages': ['Python', 'JavaScript'], 'rating': 4.5, 'active': True, 'notes': None}# ������ json.dumps() ������������������ JSON ���������json_str = json.dumps(data)# ������������print("���������������repr������������", repr(data))print("JSON ���������������", json_str)
���������������
���������������repr������������ {'active': True, 'notes': None, 'rating': 4.5, 'url': 'http://www.codercto.com', 'name': 'Codercto', 'anguages': ['Python', 'JavaScript']}JSON ��������������� {"active": true, "notes": null, "rating": 4.5, "url": "http://www.codercto.com", "name": "Codercto", "anguages": ["Python", "JavaScript"]}
���������������������������������json.dumps()
������������������������ Python ������������������������ JSON ���������������������������������������������������
������������������
������ JSON ������������������������ Python��������������� json.loads()
���������
import json# ��������������� JSON ������������������������data = json.loads(json_str)# ������������������������print("data['name']: ", data['name'])print("data['url']: ", data['url'])
���������������
data['name']: Coderctodata['url']: http://www.codercto.com
������������������
���������������������json.dump()
��� json.load()
��������������������������������������������������������������������� JSON ���������
# ������ JSON ������with open('data.json', 'w') as file: json.dump(data, file)# ������ JSON ������������with open('data.json', 'r') as file: decoded_data = json.load(file)# ������������print("���������������������", decoded_data)
������
��������������������������������� json
��������� Python ��������������������� JSON ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
