最近遇到一个json字符串解析失败的问题,原因是json里面的":这个两个符合和一些特殊符号在搞鬼,报错如下:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: …
json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: …
json.decoder.JSONDecodeError: Expecting ‘:’ delimiter: …
请对号入座,废话不多说,直接上代码,可以直接使用:
#coding=utf-8
import json
import re
import sys
sys.setrecursionlimit(1000000)
def deal_json_invaild(text):
if type(text) != str:
raise Exception("参数接受的是字符串类型")
# text = re.search(r"\{.*\}", text).group()
text = re.sub(r"\n|\t|\r|\r\n|\n\r|\x08|\\", "", text)
try:
json.loads(text)
except json.decoder