我正在解析的json位于这个URL https://reqres.in/api/users?page=2。我使用下面的代码来解析它。
Option Explicit
Sub Test_LateBinding()
Dim objRequest As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Set objRequest = CreateObject("MSXML2.XMLHTTP")
strUrl = "https://reqres.in/api/users?page=2"
blnAsync = True
With objRequest
.Open "GET", strUrl, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.Send
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strResponse = .ResponseText
End With
Debug.Print strResponse
End Sub
我可以成功地将json放入strResponse变量中。但是假设我想要一个与"Eve“相等的变量,该变量位于json字符串中的名字之下。如何从json字符串中设置变量firstName = "Eve“。
发布于 2018-09-20 06:10:31
如果你需要在VBA中使用JSON,那么我建议你使用这个库:
https://github.com/VBA-tools/VBA-JSON
使用该库的一个简单示例:
Public Sub Tester()
Dim http As Object, JSON As Object, d
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://reqres.in/api/users?page=2", False
http.SetRequestHeader "Content-Type", "application/json"
http.Send
Set JSON = ParseJson(http.responseText)
For Each d In JSON("data")
Debug.Print d("id"), d("first_name")
Next
End Sub
https://stackoverflow.com/questions/52414592
复制相似问题