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

如何用Dart重构JSON对象以适配JSON Path解析需求

使用Dart标准化JSON响应结构,方便JSON Path解析

我需要标准化JSON响应的键值对,让集成工具更易解析和遍历。

原始JSON结构

{
  "status": true,
  "body": {
    "phone": "+1 937-830-1167",
    "address": "2323 kuhku",
    "linkedin": "uhku",
    "twitter": "uhukh",
    "education": "weeww",
    "work_experience": "wewaew",
    "write_something_about_you": "yugtyt",
    "why_you_think_you_are_good_for_this_job": "kuhhuk",
    "write_your_assignment_question": "kuhghuhghj",
    "upload_your_attachment": null,
    "upload_your_resume_here": null
  }
}

目标JSON结构

将body中的键值对转换为包含label和answer字段的answers数组,方便通过JSON Path解析:

{
  "status": true,
  "body": {
    "answers": [
      {
        "label": "phone",
        "answer": "+1 937-830-1167"
      },
      {
        "label": "address",
        "answer": "2323 kuhku"
      },
      {
        "label": "linkedin",
        "answer": "uhku"
      },
      {
        "label": "twitter",
        "answer": "uhukh"
      },
      {
        "label": "education",
        "answer": "weeww"
      },
      {
        "label": "work_experience",
        "answer": "wewaew"
      },
      {
        "label": "write_something_about_you",
        "answer": "yugtyt"
      },
      {
        "label": "why_you_think_you_are_good_for_this_job",
        "answer": "kuhhuk"
      },
      {
        "label": "write_your_assignment_question",
        "answer": "kuhghuhghj"
      },
      {
        "label": "upload_your_attachment",
        "answer": null
      },
      {
        "label": "upload_your_resume_here",
        "answer": null
      }
    ]
  }
}

Dart实现代码

import 'dart:convert';

void main() {
  // 原始JSON字符串
  final rawJson = '''
{
  "status": true,
  "body": {
    "phone": "+1 937-830-1167",
    "address": "2323 kuhku",
    "linkedin": "uhku",
    "twitter": "uhukh",
    "education": "weeww",
    "work_experience": "wewaew",
    "write_something_about_you": "yugtyt",
    "why_you_think_you_are_good_for_this_job": "kuhhuk",
    "write_your_assignment_question": "kuhghuhghj",
    "upload_your_attachment": null,
    "upload_your_resume_here": null
  }
}
''';

  // 解析原始JSON为Map
  final rawData = jsonDecode(rawJson) as Map<String, dynamic>;

  // 转换body键值对为answers数组
  final answers = <Map<String, dynamic>>[];
  (rawData['body'] as Map<String, dynamic>).forEach((key, value) {
    answers.add({
      'label': key,
      'answer': value
    });
  });

  // 构建目标JSON结构
  final targetData = {
    'status': rawData['status'],
    'body': {
      'answers': answers
    }
  };

  // 输出格式化后的目标JSON
  print(jsonEncode(targetData));
}

代码说明

  1. 导入依赖:使用dart:convert库处理JSON的编解码操作。
  2. 解析原始JSON:将原始JSON字符串转换为Dart的Map对象,便于遍历和修改。
  3. 生成answers数组:遍历原始body中的所有键值对,将每个键作为label,对应值作为answer,存入数组中。
  4. 构建目标结构:按照需求组装新的JSON结构,保留原有的status字段,替换body为包含answers数组的对象。
  5. 输出结果:将目标结构编码为JSON字符串并打印,可直接用于集成工具。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:15:47