json - VBA 解析来自 API 的字符串响应
问题描述
我从 API 收到以下类型的响应:
"{"success":false,"error":"不正确的 apikey"}"
"{"success":true,"error":"正确的 apikey"}"
在 VBA 中,如何知道响应是真还是假?
我当前的代码在下面,并且在hReq.ResponseText中得到响应,并且它并不总是固定的(可以将更多键添加到 json)。虽然下面的部分是固定的,只有当它返回 true 或 false 时我才必须得到它。
"{"成功":false
代码:
Dim hReq As Object
Dim strUrl As String
strUrl = "https://myWebAPI/myMethod?apikey=123456"
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.Send
End With
MsgBox hReq.ResponseText
我知道这是基本问题,但很抱歉是 VBA 的新手。
解决方案
完全基于您所展示的内容,您可以获得 True/False
Split(Split(hReq.ResponseText, ":")(1), ",")(0)
推荐阅读
- javascript - Chrome 最终关闭 - React JS
- c# - 有没有办法在建议 MVC 控制器的方法中有一个字符串参数?
- c# - 如何使用角色 ARN 连接到 S3 存储桶是 C#
- sharepoint-online - 如何使用 pnp.sp 人员选取器设置 SharePoint 列表字段
- regex - 正则表达式环视
- c# - 如何在 .net core 2.2 中使用依赖注入添加 IEndpointsOperations 接口
- c++ - 指向特定的继承
- css - 如何避免分数 rem 值小于 1px?
- angular - 如何使用 agm-core 从主干管理多边形数据同步?
- javascript - 当我尝试将其登录到控制台时,未定义错误“r”