一只斗牛犬
在没有任何内容假设的情况下在具有混合内容的文件中查找 JSON 对象的更强大的解决方案(非 JSON 内容可能包含不成对的大括号,而 JSON 内容可能包含包含不成对的大括号等的字符串)将是遍历左括号右侧的每次出现{和每次出现的},并尝试将括号之间的子字符串解析为 JSON:import jsonright_indices = [i for i, c in enumerate(s) if c == '}']i = 0while i < len(s) - 1: if s[i] == '{': for j in right_indices: if i < j: try: print(json.loads(s[i: j + 1])) i = j + 1 break except json.decoder.JSONDecodeError: pass i += 1给定您在变量中的输入字符串s,输出:{'1': 'one', '2': 'two', '3': {'31': {'311': 'threeoneone', '312': 'threeonetwo', '313': 'threeonethree'}}, '4': {'41': 'fourone', '42': 'fourtwo', '43': 'fourthree'}, '5': 'five', '6': 'six'}
忽然笑
您可以通过识别 json 来使用正则表达式,例如:import reimport jsontext = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor.{ "1":"one", "2":"two", "3":{ "31":{ "311":"threeoneone", "312":"threeonetwo", "313":"threeonethree" } }, "4":{ "41":"fourone", "42":"fourtwo", "43":"fourthree" }, "5":"five", "6":"six"}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor."""result = re.search(r'[a-zA-Z0-9 ,.\n]+(\{[a-zA-Z0-9 \":\{\},\n]+\})[a-zA-Z0-9 ,.\n]+', text)try: json_string = result.group(1) json_data = json.loads(json_string) print(json_data)except IndexError: print("No json found!")