def is_json(msg):
# 首先判断是否是字符串
if isinstance(msg, str):
# 在这里先判断是否为数字类型的数据
try:
isinstance(int(msg), int)
return ("这个不是json类型数据")
except:
pass
try:
# 其次进行转换成python自带的数据类型
json.loads(msg)
return ("这个是json类型数据")
except ValueError:
return ("这个不是json类型数据")
else:
return ("这个不是json类型数据")
In the above code, why do we first judge whether the returned string can be converted to an int type?
Because practice has proved that "123" can also pass the above verification (as shown in the figure below)
So to optimize for this