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

Excel VBA构建API POST请求Body遇阻,求生成目标JSON结构方案

解决方法

你需要构建嵌套的层级结构来匹配目标JSON格式:外层是包含query键的对象,query的值是一个数组,数组内是包含name键的对象,且name的值要带前缀=。

以下是修正后的代码:

Dim json_dict As Dictionary
Dim query_item As Dictionary
Dim query_collection As Collection

' 创建单个查询项的字典
Set query_item = New Dictionary
query_item("name") = "=John Doe" ' 注意添加要求的前缀=

' 创建存放查询项的集合(对应JSON的数组结构)
Set query_collection = New Collection
query_collection.Add query_item

' 创建外层主字典
Set json_dict = New Dictionary
json_dict("query") = query_collection

' 转换为JSON,Whitespace参数用于生成格式化的输出(可选)
TempTxt = JsonConverter.ConvertToJson(json_dict, Whitespace:=2)
MsgBox TempTxt

运行代码后生成的JSON会完全符合你的需求:

{
  "query": [
    {
      "name": "=John Doe"
    }
  ]
}

关键说明

  • 用VBA的Collection模拟JSON中的数组,JsonConverter会自动将其转换为[]包裹的数组结构
  • 内层字典负责生成数组里的单个对象{"name": "=John Doe"}
  • 外层字典将query键与数组关联,搭建出目标JSON的层级

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:04:59