3 回答
TA贡献1784条经验 获得超2个赞
在没有任何内容假设的情况下在具有混合内容的文件中查找 JSON 对象的更强大的解决方案(非 JSON 内容可能包含不成对的大括号,而 JSON 内容可能包含包含不成对的大括号等的字符串)将是遍历左括号右侧的每次出现{和每次出现的},并尝试将括号之间的子字符串解析为 JSON:
import json
right_indices = [i for i, c in enumerate(s) if c == '}']
i = 0
while 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'}
TA贡献1735条经验 获得超5个赞
假设 JSON 没有格式错误,并且假设花括号内的所有内容都是 JSON 对象:
jsons = []
with open(f) as o:
parse_to_json = ""
for line in o:
if line == "{":
parsing_json_flag = True
if parsing_json_flag:
parse_to_json += line
if line == "}":
parsing_json_flag = False
parse_to_json = ""
jsons.append(parse_to_json)
现在,jsons使用您最喜欢的 JSON 解析库转换数组中的所有字符串。
TA贡献1806条经验 获得超5个赞
您可以通过识别 json 来使用正则表达式,例如:
import re
import json
text = """
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!")
添加回答
举报