import re def remove_comments(json_str): # 移除 // 和 /* */ 注释 pattern = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"' regex = re.compile(pattern, re.DOTALL | re.MULTILINE) def replacer(match): s = match.group(0) if s.startswith('/'): return '' return s return regex.sub(replacer, json_str)