json 是 Python 內建的模組,用於 JSON (JavaScript Object Notation) 的讀寫與轉換。
它常被用於:
- Web API 資料交換(如 RESTful API)
- 檔案儲存設定或結果
- Python 與 JavaScript 的資料交互
Python 與 JSON 的資料型別對應如下:
| Python 型別 | JSON 型別 |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float | number |
| True | true |
| False | false |
| None | null |
import json
data = {"name": "Alice", "age": 25, "city": "Taipei"}
json_str = json.dumps(data)
print(json_str)
# 輸出: '{"name": "Alice", "age": 25, "city": "Taipei"}'indent=4:輸出格式化 JSON 字串。sort_keys=True:依鍵名排序。ensure_ascii=False:允許輸出中文。
print(json.dumps(data, indent=4, ensure_ascii=False))json_str = '{"name": "Bob", "age": 30}'
data = json.loads(json_str)
print(data["name"]) # Bobwith open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)適合直接輸出到檔案。
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
print(data)常用於讀取設定檔、API 回應等。
當 JSON 格式錯誤時,會拋出 json.JSONDecodeError。
from json import loads, JSONDecodeError
try:
data = loads('{name: "Bob"}') # 缺少引號,錯誤
except JSONDecodeError as e:
print("無效的 JSON 格式:", e)config = {"version": 1.2, "debug": True, "output": "result.txt"}
with open('config.json', 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2)import requests, json
response = requests.get('https://api.github.com/users/octocat')
data = json.loads(response.text)
print(data['login'])若物件非原生 JSON 格式,可用 default 參數自訂轉換:
import json, datetime
def encoder(obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError(f'Type {type(obj)} not serializable')
now = {"time": datetime.datetime.now()}
print(json.dumps(now, default=encoder, indent=2))| 函式 | 功能 | 常用場景 |
|---|---|---|
json.dumps() |
Python → JSON 字串 | 傳輸、日誌 |
json.loads() |
JSON 字串 → Python | API 回應解析 |
json.dump() |
Python → JSON 檔案 | 儲存設定或結果 |
json.load() |
JSON 檔案 → Python | 讀取設定檔 |
JSON 模組是 Python 與外部世界溝通的核心之一,熟練掌握它能顯著提升資料處理與系統整合能力。