解析Json字符串vb6 [重复]

x3naxklr  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(199)

此问题已在此处有答案

Parsing JSON in Excel VBA(11个回答)
8天前关闭
我的代码来检索STRINGA:

Private Sub RestExample()

    Dim APICall As String
    Dim Query As String
    Dim strKey As String, STRINGA AS STRING
    Dim myXML As New MSXML2.DOMDocument60
    Dim nodes As IXMLDOMSelection

    APICall = "https://oauth.openapi.it/counters/total"
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", APICall, False
        .setRequestHeader "Authorization", "Basic " & PASSWORD
        .send
        STRINGA = .responseText
    End With

End Sub

最后我在STRINGA:

{"data":{"GET:oauth.openapi.it\/counters":{"counter":22,"paid":0,"limit":false},"GET:oauth.openapi.it\/scopes":{"counter":6,"paid":0,"limit":false},"POST:oauth.openapi.it\/token":{"counter":1,"paid":0,"limit":false},"GET:imprese.openapi.it\/advance":{"counter":14,"paid":0,"limit":false},"GET:imprese.openapi.it\/base":{"counter":2,"paid":0,"limit":false}},"success":true,"message":"","error":null}

如何从Json节点获取所有值?

tyky79it

tyky79it1#

下载VB-JSON: A Visual Basic 6 (VB6) JSON Parser Class Library并将必要的项添加到项目中。添加后,您可以像这样解析JSON字符串:

Dim p As Object
Set p = JSON.parse(STRINGA)

你可以像这样检索值

Debug.Print p.Item("data").Item("GET:oauth.openapi.it\/counters").Item("counter")
Debug.Print p.Item("success")
Debug.Print p.Item("message")
Debug.Print p.Item("error")

或者,如果你想迭代这些项,可以这样做:

Dim Data As Variant
Dim Item As Variant
   
For Each Data In p.Item("data").Items
   For Each Item In Data.Items
      Debug.Print Item
   Next
Next

相关问题