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

VBA调用API后如何正确提取返回JSON数据中的公司名称字段

VBA提取API返回JSON中公司名称的修复方案

问题原因

你当前代码的核心错误是没有匹配JSON的实际层级结构:

  • API返回的JSON根节点是对象类型,包含状态码、返回信息、业务数据三类属性,并非直接存储公司数据的数组,直接遍历根对象无法获取公司字段。

修复后代码

' 解析JSON
Dim response As Object
Set response = JsonConverter.ParseJson(request.ResponseText)

' 取根节点下的data字段,该字段对应存储公司列表的数组
Dim compList As Variant
Set compList = response("data")
' 若data下还有一层数组容器(如items/records),则改为 Set compList = response("data")("items")

Dim comp As Variant
For Each comp In compList
    Debug.Print comp("name")
Next comp

层级调试方案

如果执行后仍无法获取数据,可先运行以下调试代码确认JSON结构:

  1. 确认data字段类型
Debug.Print TypeName(response("data"))
  • 输出Collection:说明data就是公司列表数组,上述代码可直接使用
  • 输出Dictionary:说明data下还有一层数组字段,可打印所有key确认字段名:
Dim key As Variant
For Each key In response("data").Keys
    Debug.Print key
Next

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 14:45:04