JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Error when converting conversion

import json
tt = "{'start_row': 0, 'end_row': 0, 'env': 0, 'now': 0}"
t = json.loads(tt)
print(t)

Solution one

Turn single quotes into double quotes

import json
tt = '{"start_row": 0, "end_row": 0, "env": 0, "now": 0}'
t = json.loads(tt)
print(t)

Solution two

import ast
tt = "{'start_row': 0, 'end_row': 0, 'env': 0, 'now': 0}"
tt = ast.literal_eval(tt)
print(tt, type(tt))

Other errors

json.decoder.JSONDecodeError: Invalid control character at: line 2 column 18 (char 19)
Reason: json uses a rigorous format by default, and it is easy to report this error when transferring data across languages.
Solution: add the parameter strict

json.loads(json_data, strict=False)

json.dumps(data) Convert Chinese characters to unicode encoding
Reason: json will perform character conversion by default.
Solution: Add the ensure_ascii parameter

json.dumps(data, ensure_ascii=False)

Guess you like

Origin blog.csdn.net/a12355556/article/details/115287435