Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Chuyển string thành JSON dùng Python

Chuyển JSON string thành dictionary dùng json.loads(). Method nhận valid json string trả về dictionary truy cập tất cả element. Ví dụ:

>>> import json
>>> s = '{"success": "true", "status": 200, "message": "Hello"}'
>>> d = json.loads(s)
>>> print d["success"], d["status"]
true 200

Nếu có json file thay vì string, dùng json.load() method.