首页 > 解决方案 > 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 的新手。

标签: jsonvbaexcelhttp

解决方案


完全基于您所展示的内容,您可以获得 True/False

Split(Split(hReq.ResponseText, ":")(1), ",")(0)

推荐阅读