2024-10-18 18:06:23 +08:00
|
|
|
|
|
2024-08-29 16:37:09 +08:00
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
|
2024-10-18 18:06:23 +08:00
|
|
|
|
def insert_missing_commas(json_str):
|
|
|
|
|
"""
|
|
|
|
|
使用正则表达式在缺失逗号的位置插入逗号。
|
|
|
|
|
具体来说,寻找一个值的结束引号后紧跟着下一个键的开始引号,并在中间插入逗号。
|
|
|
|
|
"""
|
|
|
|
|
# 这个正则匹配一个字符串结尾的引号,可能有空白字符,然后是另一个键的引号
|
|
|
|
|
pattern = r'(":\s*"[^"]*)"\s*(")'
|
|
|
|
|
replacement = r'\1", \2'
|
|
|
|
|
|
|
|
|
|
previous_str = None
|
|
|
|
|
while previous_str != json_str:
|
|
|
|
|
previous_str = json_str
|
|
|
|
|
json_str = re.sub(pattern, replacement, json_str)
|
|
|
|
|
|
|
|
|
|
return json_str
|
2024-11-26 15:06:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fix_json_escape_sequences(json_str):
|
|
|
|
|
"""
|
|
|
|
|
修复 JSON 字符串中的非法转义序列。
|
|
|
|
|
将所有不符合 JSON 规范的反斜杠进行转义。
|
|
|
|
|
"""
|
|
|
|
|
# JSON 中合法的转义字符
|
|
|
|
|
valid_escapes = ['"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u']
|
|
|
|
|
|
|
|
|
|
# 使用正则表达式找到所有反斜杠
|
|
|
|
|
# 如果反斜杠后面不是合法的转义字符,则进行转义
|
|
|
|
|
pattern = re.compile(r'\\(?!["\\/bfnrtu])')
|
|
|
|
|
fixed_str = pattern.sub(r'\\\\', json_str)
|
|
|
|
|
return fixed_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_latex_expressions(json_str):
|
|
|
|
|
"""
|
|
|
|
|
替换 JSON 字符串中的 LaTeX 风格表达式。
|
|
|
|
|
例如,将 $三 \geq 2 m$ 替换为 三 ≥2米
|
|
|
|
|
"""
|
|
|
|
|
# 定义 LaTeX 符号到 Unicode 字符的映射
|
|
|
|
|
latex_mapping = {
|
|
|
|
|
r'\geq': '≥',
|
|
|
|
|
r'\leq': '≤',
|
|
|
|
|
r'\times': '×',
|
|
|
|
|
r'\frac': '/', # 简单处理分数
|
|
|
|
|
r'\neq': '≠',
|
|
|
|
|
r'\approx': '≈',
|
|
|
|
|
r'\pm': '±',
|
|
|
|
|
r'\alpha': 'α',
|
|
|
|
|
r'\beta': 'β',
|
|
|
|
|
r'\gamma': 'γ',
|
|
|
|
|
r'\delta': 'δ',
|
|
|
|
|
r'\pi': 'π',
|
|
|
|
|
r'\sqrt': '√',
|
|
|
|
|
r'\infty': '∞',
|
|
|
|
|
r'\cup': '∪',
|
|
|
|
|
r'\cap': '∩',
|
|
|
|
|
r'\subseteq': '⊆',
|
|
|
|
|
r'\supseteq': '⊇',
|
|
|
|
|
r'\forall': '∀',
|
|
|
|
|
r'\exists': '∃',
|
|
|
|
|
r'\rightarrow': '→',
|
|
|
|
|
r'\leftarrow': '←',
|
|
|
|
|
# 添加更多需要的映射
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 处理每一个 LaTeX 表达式
|
|
|
|
|
def replace_match(match):
|
|
|
|
|
expr = match.group(1)
|
|
|
|
|
for latex, char in latex_mapping.items():
|
|
|
|
|
expr = expr.replace(latex, char)
|
|
|
|
|
# 替换单位符号,例如 ' m' 到 '米', ' s' 到 '秒', 等等
|
|
|
|
|
expr = re.sub(r'(\d+)\s*m', r'\1米', expr)
|
|
|
|
|
expr = re.sub(r'(\d+)\s*s', r'\1秒', expr)
|
|
|
|
|
expr = re.sub(r'(\d+)\s*kg', r'\1公斤', expr)
|
|
|
|
|
expr = re.sub(r'(\d+)\s*A', r'\1安', expr) # 例如电流单位安培
|
|
|
|
|
# 继续添加更多单位
|
|
|
|
|
return expr
|
|
|
|
|
|
|
|
|
|
# 替换所有 $...$ 包围的内容
|
|
|
|
|
fixed_str = re.sub(r'\$(.*?)\$', replace_match, json_str)
|
|
|
|
|
return fixed_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_content_from_json(string):
|
|
|
|
|
"""
|
|
|
|
|
输入字符串,尝试解析 JSON 数据:
|
|
|
|
|
1. 尝试直接解析原始 JSON。
|
|
|
|
|
2. 如果直接解析失败,按顺序使用不同修复方法尝试解析。
|
|
|
|
|
"""
|
|
|
|
|
if not string.strip():
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
# 提取第一个匹配的 JSON 对象
|
|
|
|
|
match = re.search(r'\{[\s\S]*\}', string)
|
|
|
|
|
if not match:
|
|
|
|
|
print("未找到有效的 JSON 内容。")
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
original_json = match.group(0)
|
|
|
|
|
|
|
|
|
|
# 尝试直接解析原始 JSON 数据
|
|
|
|
|
try:
|
|
|
|
|
parsed = json.loads(original_json)
|
2024-11-27 16:58:25 +08:00
|
|
|
|
# print("直接解析原始 JSON 成功。")
|
2024-11-26 15:06:57 +08:00
|
|
|
|
return parsed
|
|
|
|
|
except json.JSONDecodeError as original_error:
|
|
|
|
|
print(f"直接解析原始 JSON 失败: {original_error}")
|
|
|
|
|
|
|
|
|
|
# 方法1:逗号修复
|
|
|
|
|
try:
|
|
|
|
|
fixed_json1 = insert_missing_commas(original_json)
|
|
|
|
|
parsed = json.loads(fixed_json1)
|
|
|
|
|
print("使用方法1:逗号修复成功。")
|
|
|
|
|
return parsed
|
|
|
|
|
except json.JSONDecodeError as error1:
|
|
|
|
|
print(f"方法1(逗号修复)解析失败: {error1}")
|
|
|
|
|
|
|
|
|
|
# 方法2:LaTeX 表达式替换
|
|
|
|
|
try:
|
|
|
|
|
fixed_json2 = replace_latex_expressions(original_json)
|
|
|
|
|
parsed = json.loads(fixed_json2)
|
|
|
|
|
print("使用方法2:LaTeX 表达式替换成功。")
|
|
|
|
|
return parsed
|
|
|
|
|
except json.JSONDecodeError as error2:
|
|
|
|
|
print(f"方法2(LaTeX 替换)解析失败: {error2}")
|
|
|
|
|
|
|
|
|
|
# 方法3:非法转义序列修复
|
|
|
|
|
try:
|
|
|
|
|
fixed_json3 = fix_json_escape_sequences(original_json)
|
|
|
|
|
parsed = json.loads(fixed_json3)
|
|
|
|
|
print("使用方法3:非法转义序列修复成功。")
|
|
|
|
|
return parsed
|
|
|
|
|
except json.JSONDecodeError as error3:
|
|
|
|
|
print(f"方法3(非法转义修复)解析失败: {error3}")
|
|
|
|
|
|
|
|
|
|
# 如果所有方法都失败,返回空字典
|
|
|
|
|
print("所有修复方法均失败,返回空字典。")
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_latex_expressions_in_dict(obj):
|
|
|
|
|
"""
|
|
|
|
|
递归遍历字典或列表,替换其中的 LaTeX 表达式。
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
|
return {k: replace_latex_expressions_in_dict(v) for k, v in obj.items()}
|
|
|
|
|
elif isinstance(obj, list):
|
|
|
|
|
return [replace_latex_expressions_in_dict(item) for item in obj]
|
|
|
|
|
elif isinstance(obj, str):
|
|
|
|
|
# 仅处理被 $...$ 包围的内容
|
|
|
|
|
def replace_match(match):
|
|
|
|
|
expr = match.group(1)
|
|
|
|
|
return replace_latex_expressions(expr)
|
|
|
|
|
|
|
|
|
|
# 替换所有 $...$ 包围的内容
|
|
|
|
|
return re.sub(r'\$(.*?)\$', replace_match, obj)
|
|
|
|
|
else:
|
|
|
|
|
return obj
|
2024-08-29 16:37:09 +08:00
|
|
|
|
def clean_json_string(json_string):
|
2024-10-27 17:16:29 +08:00
|
|
|
|
# print(json_string)
|
2024-08-29 16:37:09 +08:00
|
|
|
|
"""清理JSON字符串,移除多余的反引号并解析为字典"""
|
|
|
|
|
return extract_content_from_json(json_string)
|
|
|
|
|
|
2024-10-19 17:25:56 +08:00
|
|
|
|
|
2024-08-29 16:37:09 +08:00
|
|
|
|
def combine_json_results(json_lists):
|
|
|
|
|
"""
|
2024-10-19 17:25:56 +08:00
|
|
|
|
将类json格式的列表整合成json数据(即大括号{}包裹)。
|
|
|
|
|
支持列表中的元素既是字符串又是字典。
|
2024-08-29 16:37:09 +08:00
|
|
|
|
"""
|
|
|
|
|
combined_result = {}
|
2024-10-19 17:25:56 +08:00
|
|
|
|
for item in json_lists:
|
|
|
|
|
if isinstance(item, str):
|
|
|
|
|
if item.strip():
|
|
|
|
|
json_data = clean_json_string(item)
|
|
|
|
|
if isinstance(json_data, dict):
|
|
|
|
|
combined_result.update(json_data)
|
|
|
|
|
else:
|
|
|
|
|
print(f"警告: 解析后的数据不是字典类型,跳过。内容: {item}")
|
|
|
|
|
elif isinstance(item, dict):
|
|
|
|
|
combined_result.update(item)
|
|
|
|
|
else:
|
|
|
|
|
print(f"警告: 不支持的类型 {type(item)},跳过。内容: {item}")
|
2024-08-29 16:37:09 +08:00
|
|
|
|
return combined_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def nest_json_under_key(data, key):
|
|
|
|
|
"""
|
|
|
|
|
将给定的字典 data 嵌套在一个新的字典层级下,该层级由 key 指定,并返回 JSON 格式的字符串。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
- data: dict, 要嵌套的原始字典。
|
|
|
|
|
- key: str, 新层级的键名。
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
- 嵌套后的 JSON 字符串。
|
|
|
|
|
"""
|
|
|
|
|
# 创建一个新字典,其中包含一个键,该键的值是原始字典
|
|
|
|
|
nested_dict = {key: data}
|
|
|
|
|
# 将字典转换成 JSON 字符串
|
|
|
|
|
nested_json = json.dumps(nested_dict, ensure_ascii=False, indent=4)
|
|
|
|
|
return nested_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_keys_to_json(target_dict, source_dict):
|
|
|
|
|
"""
|
|
|
|
|
将 source_dict 的内容添加到 target_dict 中的唯一外层键下的字典中。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
target_dict (dict): 要更新的目标字典,假定只有一个外层键。
|
|
|
|
|
source_dict (dict): 源字典,其内容将被添加到目标字典。
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
dict: 更新后的字典。
|
|
|
|
|
"""
|
|
|
|
|
if not target_dict:
|
2024-09-13 15:03:55 +08:00
|
|
|
|
print("json_utils: Error: Target dictionary is empty.")
|
2024-08-29 16:37:09 +08:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
if len(target_dict) != 1:
|
2024-09-13 15:03:55 +08:00
|
|
|
|
print("json_utils: Error: Target dictionary must contain exactly one top-level key.")
|
2024-08-29 16:37:09 +08:00
|
|
|
|
return target_dict
|
|
|
|
|
|
|
|
|
|
# 获取唯一的外层键
|
|
|
|
|
target_key, existing_dict = next(iter(target_dict.items()))
|
|
|
|
|
|
|
|
|
|
if not isinstance(existing_dict, dict):
|
2024-09-13 15:03:55 +08:00
|
|
|
|
print(f"json_utils: Error: The value under the key '{target_key}' is not a dictionary.")
|
2024-08-29 16:37:09 +08:00
|
|
|
|
return target_dict
|
|
|
|
|
|
|
|
|
|
# 合并字典
|
|
|
|
|
existing_dict.update(source_dict)
|
|
|
|
|
|
|
|
|
|
# 更新原字典
|
|
|
|
|
target_dict[target_key] = existing_dict
|
|
|
|
|
|
|
|
|
|
return target_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rename_outer_key(original_data,new_key):
|
|
|
|
|
# 定义新的键名
|
|
|
|
|
# new_key = "重新招标, 不再招标和终止招标"
|
|
|
|
|
|
|
|
|
|
# 提取原始数据中的唯一外层值(假设只有一个外层键)
|
|
|
|
|
if not original_data or not isinstance(original_data, dict):
|
2024-09-13 15:03:55 +08:00
|
|
|
|
print("json_utils: Error: Invalid input or input is not a dictionary.") # 如果输入无效或不是字典,则返回空字典
|
2024-09-11 12:02:09 +08:00
|
|
|
|
return {}
|
2024-08-29 16:37:09 +08:00
|
|
|
|
|
|
|
|
|
# 使用 next(iter(...)) 提取第一个键的值
|
|
|
|
|
original_value = next(iter(original_data.values()), {})
|
|
|
|
|
|
|
|
|
|
# 创建一个新的字典,使用新的键名
|
|
|
|
|
new_data = {new_key: original_value}
|
|
|
|
|
|
2024-10-15 20:57:58 +08:00
|
|
|
|
return new_data
|
2024-08-29 16:37:09 +08:00
|
|
|
|
def transform_json_values(data):
|
|
|
|
|
if isinstance(data, dict):
|
2024-10-16 20:18:55 +08:00
|
|
|
|
return {key.replace(' ', ''): transform_json_values(value) for key, value in data.items()}
|
2024-08-29 16:37:09 +08:00
|
|
|
|
elif isinstance(data, list):
|
|
|
|
|
return [transform_json_values(item) for item in data]
|
|
|
|
|
elif isinstance(data, bool):
|
|
|
|
|
return '是' if data else '否'
|
|
|
|
|
elif isinstance(data, (int, float)):
|
|
|
|
|
return str(data)
|
|
|
|
|
elif isinstance(data, str):
|
|
|
|
|
return data.replace('\n', '<br>')
|
|
|
|
|
else:
|
|
|
|
|
return data
|