`json.loads()` / `json.load()` 有個 `strict` 參數,**預設 `True`**。它只管一件事:
**字串值裡能不能出現「未跳脫的控制字元」**——也就是「換行字元本身、tab 字元本身」
(按 Enter/Tab 直接打進去的那個字元),而不是 `\n`、`\t` 這種跳脫寫法。
json.loads(s, *, strict=True) # 預設
一個例子看懂
import json
# 字串值裡有「換行字元本身」(按 Enter 打進去的,不是 \n 跳脫寫法)→ 不合法 JSON
bad = '{"description": "line1\nline2"}'
json.loads(bad) # ❌ JSONDecodeError: Invalid control character...
json.loads(bad, strict=False) # ✅ {'description': 'line1\nline2'}
正確做法是把換行「跳脫」成 \n(兩個字元 \ + n),這樣連 strict=True 都沒問題:
good = '{"description": "line1\\nline2"}' # 合法 JSON
json.loads(good) # ✅ 不必 strict=False





![Python `typing.NamedTuple` (`collections.namedtuple`) 與 `typing.Literal` 教學 — 用型別「防止錯配」; StripRule = NamedTuple(“StripRule”, [(“regex”, re.Pattern), (“flag”, str)]) vs StripRule = namedtuple(“StripRule”, [“regex”, “flag”]) Python `typing.NamedTuple` (`collections.namedtuple`) 與 `typing.Literal` 教學 — 用型別「防止錯配」; StripRule = NamedTuple(“StripRule”, [(“regex”, re.Pattern), (“flag”, str)]) vs StripRule = namedtuple(“StripRule”, [“regex”, “flag”])](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2026/07/20260702150603_0_86abc2.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言