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

如何遍历IBM Watson语音转文本API的同名JSON数组直至满足条件?

解决IBM Watson语音转文本API时间戳遍历问题

嘿,我来帮你理清楚这个问题~首先得纠正一个小误解:Stack Overflow说的“不允许多个同名字段”,指的是同一个JSON对象里不能存在重复的键(比如{ "alternatives": x, "alternatives": y }这种自相矛盾的结构),但你给出的Watson返回结构是完全合法的!因为results是一个数组,数组里的每个元素都是独立的对象,每个对象里的alternatives是各自的数组——它们分属不同对象,完全符合JSON规范,所以在线工具验证有效是正常的。

接下来咱们说怎么遍历timestamps直到满足指定条件(比如找到包含20秒00毫秒的条目),我给你两种常用语言的实现示例:

JavaScript实现

// 模拟Watson返回的JSON响应
const watsonResponse = {
  "results": [
    {
      "alternatives": [
        {
          "timestamps": [
            ["hello", 1.2, 1.8],
            ["world", 1.9, 2.5],
            // 假设这里有跨20秒的条目
            ["target", 19.9, 20.3]
          ]
        }
      ]
    },
    {
      "alternatives": [
        {
          "timestamps": [
            ["another", 20.0, 20.5]
          ]
        }
      ]
    }
  ]
};

// 目标时间:20秒00毫秒(即20.0)
const targetTime = 20.0;
let foundEntry = null;

// 逐层遍历嵌套结构
for (const result of watsonResponse.results) {
  for (const alternative of result.alternatives) {
    for (const timestamp of alternative.timestamps) {
      const [word, startTime, endTime] = timestamp;
      // 判断逻辑可按需调整:比如检查目标时间是否在当前条目时间区间内
      if (startTime <= targetTime && targetTime <= endTime) {
        foundEntry = { word, startTime, endTime };
        // 找到第一个匹配项后立即终止所有循环
        break;
      }
    }
    if (foundEntry) break;
  }
  if (foundEntry) break;
}

// 输出结果
if (foundEntry) {
  console.log(`找到符合条件的条目:单词"${foundEntry.word}",时间范围 ${foundEntry.startTime} - ${foundEntry.endTime} 秒`);
} else {
  console.log("未找到符合条件的时间戳");
}

Python实现

# 模拟Watson返回的JSON响应
watson_response = {
    "results": [
        {
            "alternatives": [
                {
                    "timestamps": [
                        ["hello", 1.2, 1.8],
                        ["world", 1.9, 2.5],
                        ["target", 19.9, 20.3]
                    ]
                }
            ]
        },
        {
            "alternatives": [
                {
                    "timestamps": [
                        ["another", 20.0, 20.5]
                    ]
                }
            ]
        }
    ]
}

# 目标时间:20秒00毫秒(即20.0)
target_time = 20.0
found_entry = None

# 逐层遍历嵌套结构
for result in watson_response["results"]:
    for alternative in result["alternatives"]:
        for timestamp in alternative["timestamps"]:
            word, start_time, end_time = timestamp
            # 按需调整判断逻辑,比如精确匹配起始时间
            if start_time <= target_time <= end_time:
                found_entry = {"word": word, "start_time": start_time, "end_time": end_time}
                # 找到后终止循环
                break
        if found_entry:
            break
    if found_entry:
        break

# 输出结果
if found_entry:
    print(f'找到符合条件的条目:单词"{found_entry["word"]}",时间范围 {found_entry["start_time"]} - {found_entry["end_time"]} 秒')
else:
    print("未找到符合条件的时间戳")

额外说明

  • 你可以根据实际需求调整时间判断逻辑:比如要精确匹配20.0秒作为起始时间,就把判断条件改成startTime === targetTime(JS)或start_time == target_time(Python)。
  • 如果需要收集所有符合条件的条目,去掉循环里的break语句,把结果存入数组即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:19:34