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

如何遍历给定JSON响应体提取每条记录的transactionReference值

提取接口响应中content数组的transactionReference字段实现方案

你需要提取的交易参考号存储在响应结构的responseBody.content数组中,直接遍历该数组读取对应字段即可,以下是常用编程语言的实现示例:

结构对应关系

  • 交易记录列表路径:responseBody.content
  • 单条记录目标字段:transactionReference
  • 你提供的示例数据提取后结果为:

    ["MNFY|20191029110159|005805", "MNFY|20191029104511|005830"]

代码示例

JavaScript 实现

// 替换为你实际拿到的接口返回对象
const apiResponse = {/* 你的接口响应数据 */}
const transactionReferences = []

// 遍历交易记录数组
apiResponse.responseBody.content.forEach(record => {
  transactionReferences.push(record.transactionReference)
})

如果需要更简洁的写法,可以直接用map方法:

const transactionReferences = apiResponse.responseBody.content.map(record => record.transactionReference)

Python 实现

# 替换为你实际拿到的接口返回字典
api_response = {} # 你的接口响应数据
transaction_references = []

for record in api_response.get("responseBody", {}).get("content", []):
    transaction_references.append(record.get("transactionReference"))

注意事项

  • 遍历前建议做空值校验:判断responseBody、content是否存在,避免接口返回异常时触发代码报错
  • 该接口是分页结构(返回值包含totalPages、size、number分页参数),如果需要提取全量交易参考号,需要循环请求所有分页后再汇总结果
  • 提取字段时建议兼容字段缺失场景,避免单条记录数据异常导致整个遍历逻辑中断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 13:36:15