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

非标准JSON格式转换请求:将特殊结构数据转为规范JSON

非规范字符串转规范JSON结构处理方案

原始数据源

{'OUT': '232827@@@Price;1=9.8=0;2=4.1=1;3=7.9=0;#Needs;1=3.0=0;2=1.4=1;3=3.5=0;'}

数据源存在的问题

  • 无明确键名,仅通过分隔符区分数据项
  • 使用=作为字段值分隔符,不符合JSON标准格式
  • 单条字符串内包含Price和Needs两组独立数据集
  • 开头带有232827@@@前缀,外层仅含OUT一个键名

目标转换后的规范结构

第一组(Price相关数据)

[
  {
    "No": 1,
    "Price": 9.8,
    "status": 0
  },
  {
    "No": 2,
    "Price": 4.1,
    "status": 1
  },
  {
    "No": 3,
    "Price": 7.9,
    "status": 0
  }
]

第二组(Needs相关数据)

[
  {
    "No": 1,
    "Needs": 3.0,
    "status": 0
  },
  {
    "No": 2,
    "Needs": 1.4,
    "status": 1
  },
  {
    "No": 3,
    "Needs": 3.5,
    "status": 0
  }
]

实现代码(Python)

用Python可以快速完成解析转换:

import json

# 原始输入数据
raw_data = {'OUT': '232827@@@Price;1=9.8=0;2=4.1=1;3=7.9=0;#Needs;1=3.0=0;2=1.4=1;3=3.5=0;'}

# 提取核心内容,去掉前缀和末尾多余分号
core_content = raw_data['OUT'].split('@@@')[1].rstrip(';')

# 拆分Price和Needs两组数据
price_section, needs_section = core_content.split('#Needs;')

# 通用解析函数,处理单组数据
def parse_data_section(section, target_key):
    result = []
    # 按分号拆分每条数据
    for item in section.split(';'):
        if not item:
            continue
        # 按等号拆分No、值、状态
        no_str, value_str, status_str = item.split('=')
        result.append({
            "No": int(no_str),
            target_key: float(value_str),
            "status": int(status_str)
        })
    return result

# 解析两组数据
price_result = parse_data_section(price_section, "Price")
needs_result = parse_data_section(needs_section, "Needs")

# 输出格式化后的JSON
print("Price组转换结果:")
print(json.dumps(price_result, indent=2))
print("\nNeeds组转换结果:")
print(json.dumps(needs_result, indent=2))

代码运行结果

执行上述代码后,输出的JSON结构与目标完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 15:20:51