You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复非标准JSON字典?将单引号格式转为有效JSON格式

非标准JSON字典列表转规范格式的实现方法

针对你提供的非标准JSON文本,需要处理以下几个格式问题:

  • 每行首尾多余的双引号包裹
  • 键和字符串值使用单引号而非JSON要求的双引号
  • 可选:tasks字段后缺少冒号(这是JSON规范必须的,若你的目标格式是笔误,建议补充)

方法一:Python脚本处理

适合跨平台使用,逻辑清晰易扩展:

import re

# 替换为你的输入输出文件路径
input_path = "input.txt"
output_path = "output.txt"

with open(input_path, "r", encoding="utf-8") as infile, open(output_path, "w", encoding="utf-8") as outfile:
    for line in infile:
        # 移除首尾空白和外层双引号
        trimmed_line = line.strip().strip('"')
        # 将单引号替换为双引号
        quoted_line = trimmed_line.replace("'", '"')
        # 给tasks字段添加冒号(若不需要此步骤可删除该行)
        standard_line = re.sub(r'"tasks"\s*\[(.*?)\]', r'"tasks": [\1]', quoted_line)
        # 写入处理后的行
        outfile.write(standard_line + "\n")

方法二:命令行工具(Linux/macOS)

用sed快速批量处理,无需编写脚本:

# 基础处理(仅移除外层双引号、替换单引号)
sed -e 's/^"//' -e 's/"$//' -e "s/'/\"/g" input.txt > output.txt

# 若需要补充tasks字段的冒号,使用此命令
sed -e 's/^"//' -e 's/"$//' -e "s/'/\"/g" -e 's/"tasks"\s*\[/\"tasks\": \[/' input.txt > output.txt

命令解释:

  • s/^"//:删除行开头的双引号
  • s/"$//:删除行结尾的双引号
  • s/'/\"/g:全局替换单引号为双引号
  • s/"tasks"\s*\[/\"tasks\": \[/:匹配"tasks" [并替换为"tasks": [,补全JSON键值对的冒号

内容的提问来源于stack exchange,提问作者sylar_80

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:01:15