json_util.py 371 B

123456789101112131415
  1. import re
  2. def remove_comments(json_str):
  3. # 移除 // 和 /* */ 注释
  4. pattern = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
  5. regex = re.compile(pattern, re.DOTALL | re.MULTILINE)
  6. def replacer(match):
  7. s = match.group(0)
  8. if s.startswith('/'):
  9. return ''
  10. return s
  11. return regex.sub(replacer, json_str)