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

如何从Python API调用返回的JSON响应中提取Active Tunnels最低值

Python提取Active Tunnels字段最低值方案

前置说明

你调用API拿到的响应解析为Python字典后,histdata字段存储了所有时序数据,每条数据都包含Active Tunnels数值,我们只需要遍历该列表对比取值即可。

如果你使用requests库调用API,可直接通过response.json()获取解析完成的Python字典,无需手动调用json.loads方法。

实现代码

方式1:for循环实现(符合你提到的循环实现需求)

import json

# 替换为你实际拿到的API响应字符串
api_raw_response = "你的API返回的原始JSON字符串"
data = json.loads(api_raw_response)

min_active_tunnels = float('inf')
for entry in data["histdata"]:
    current = entry["Active Tunnels"]
    if current < min_active_tunnels:
        min_active_tunnels = current

# 输出结果
print("Active Tunnels最低值:", min_active_tunnels)

针对你给出的示例数据,输出结果为374.0。

方式2:精简写法(利用Python内置min函数)

不需要手动维护对比逻辑,代码更短:

import json

api_raw_response = "你的API返回的原始JSON字符串"
data = json.loads(api_raw_response)
min_active_tunnels = min(e["Active Tunnels"] for e in data["histdata"])
print("Active Tunnels最低值:", min_active_tunnels)

异常处理提示

如果你的场景中histdata可能为空列表,可提前加判断避免运行报错:

if not data["histdata"]:
    print("无可用数据")
else:
    # 执行上述取值逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 04:54:07