Mô-đun JSON tích hợp có thể được sử dụng làm trình xác thực:
import json
def parse[text]:
try:
return json.loads[text]
except ValueError as e:
print['invalid json: %s' % e]
return None # or: raise
Bạn có thể làm cho nó hoạt động với các tệp bằng cách sử dụng:
with open[filename] as f:
return json.load[f]
Thay vì json.loads
và bạn cũng có thể bao gồm tên tệp trong thông báo lỗi.
Trên Python 3.3.5, đối với {test: "foo"}
, tôi nhận được:
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
và trên 2.7.6:
invalid json: Expecting property name: line 1 column 2 [char 1]
Điều này là do JSON chính xác là {"test": "foo"}
.
Khi xử lý các tệp không hợp lệ, tốt nhất là không xử lý chúng thêm nữa. Bạn có thể xây dựng tệp đã bỏ qua.txt liệt kê các tệp có lỗi, để chúng có thể được kiểm tra và sửa bằng tay.
Nếu có thể, bạn nên kiểm tra trang web/chương trình tạo các tệp JSON không hợp lệ, sửa lỗi đó và sau đó tạo lại tệp JSON. Nếu không, bạn sẽ tiếp tục có các tệp mới là JSON không hợp lệ.
Không điều đó, bạn sẽ cần viết một trình phân tích cú pháp JSON tùy chỉnh để sửa lỗi phổ biến. Với điều đó, bạn nên đặt bản gốc dưới điều khiển nguồn [hoặc lưu trữ], để bạn có thể thấy và kiểm tra sự khác biệt mà công cụ tự động sửa chữa [như kiểm tra tỉnh táo]. Các trường hợp mơ hồ nên được cố định bằng tay.
Đưa ra một chuỗi python, nhiệm vụ là kiểm tra xem chuỗi có phải là đối tượng JSON hợp lệ hay không. Hãy để cố gắng hiểu vấn đề bằng cách sử dụng các ví dụ khác nhau.
Ví dụ 1: Chuỗi JSON không hợp lệ do báo giá không chính xác trong chuỗi Invalid JSON String due to incorrect quotes in Strings
Python3
with open[filename] as f:
return json.load[f]
0 with open[filename] as f:
return json.load[f]
1with open[filename] as f:
return json.load[f]
2with open[filename] as f:
return json.load[f]
3 with open[filename] as f:
return json.load[f]
4with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6with open[filename] as f:
return json.load[f]
7with open[filename] as f:
return json.load[f]
8with open[filename] as f:
return json.load[f]
9invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22with open[filename] as f:
return json.load[f]
3 invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
8invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9invalid json: Expecting property name: line 1 column 2 [char 1]
0 invalid json: Expecting property name: line 1 column 2 [char 1]
1invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6invalid json: Expecting property name: line 1 column 2 [char 1]
5invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9Output:
initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false
Giải thích: Trong ví dụ trên, chuỗi là một JSON không hợp lệ bởi vì, các ký tự chuỗi được đặt trong các trích dẫn đơn [], nhưng theo lược đồ JSON hợp lệ, các chuỗi phải được đặt trong các trích dẫn kép [Hồi]. In the above example, the string is an invalid JSON because, the string characters the enclosed in single quotes [‘], but as per valid JSON schema, strings must be enclosed in double quotes [“].
Ví dụ 2: Ví dụ về chuỗi JSON hợp lệ
Python3
with open[filename] as f:
return json.load[f]
0 with open[filename] as f:
return json.load[f]
1with open[filename] as f:
return json.load[f]
2with open[filename] as f:
return json.load[f]
3 with open[filename] as f:
return json.load[f]
4invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false4
initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false5
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
9invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
JSON String: {"name":"John", "age":31, "Salary":2500000} Is valid?: True JSON String: { "Subjects": {"Maths":85.01, "Physics":90}} Is valid?: True JSON String: {"success": true} Is valid?: True0
initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6JSON String: {"name":"John", "age":31, "Salary":2500000} Is valid?: True JSON String: { "Subjects": {"Maths":85.01, "Physics":90}} Is valid?: True JSON String: {"success": true} Is valid?: True4
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6with open[filename] as f:
return json.load[f]
7with open[filename] as f:
return json.load[f]
8initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True2
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22with open[filename] as f:
return json.load[f]
3 invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True7
JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True8
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True7
json.loads
1invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True7
json.loads
4invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9Output:
JSON String: {"name":"John", "age":31, "Salary":2500000} Is valid?: True JSON String: { "Subjects": {"Maths":85.01, "Physics":90}} Is valid?: True JSON String: {"success": true} Is valid?: True
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
8invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
with open[filename] as f:
return json.load[f]
with open[filename] as f:
return json.load[f]
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
invalid json: Expecting property name: line 1 column 2 [char 1]
0 invalid json: Expecting property name: line 1 column 2 [char 1]
1json.JSONDecoder able to parse this String.Python3
with open[filename] as f:
return json.load[f]
0 with open[filename] as f:
return json.load[f]
1with open[filename] as f:
return json.load[f]
2with open[filename] as f:
return json.load[f]
3 with open[filename] as f:
return json.load[f]
4invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false4
initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false5
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1with open[filename] as f:
return json.load[f]
9invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
JSON String: {"name":"John", "age":31, "Salary":2500000} Is valid?: True JSON String: { "Subjects": {"Maths":85.01, "Physics":90}} Is valid?: True JSON String: {"success": true} Is valid?: True0
initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6JSON String: {"name":"John", "age":31, "Salary":2500000} Is valid?: True JSON String: { "Subjects": {"Maths":85.01, "Physics":90}} Is valid?: True JSON String: {"success": true} Is valid?: True4
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9with open[filename] as f:
return json.load[f]
5 with open[filename] as f:
return json.load[f]
6with open[filename] as f:
return json.load[f]
7with open[filename] as f:
return json.load[f]
8initial string {'akshat' : 1, 'nikhil' : 2} Is valid json? false9
with open[filename] as f:
return json.load[f]
5with open[filename] as f:
return json.load[f]
6JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True2
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22with open[filename] as f:
return json.load[f]
3 invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4with open[filename] as f:
return json.load[f]
06with open[filename] as f:
return json.load[f]
3with open[filename] as f:
return json.load[f]
08Output:
JSON String: { "Person": [ {"name": "P_1", "age": 42}, {"name": "P_2", "age": 21}, {"name": "P_3", "age": 36} ], "location": "1.1.0", "Country": "IND" } Is valid?: True