无法使用vba解析JSON

时间:2016-06-18 23:59:05

标签: json excel vba excel-vba parsing

我可以将出现的作为来自网页查询的有效JSON字符串,但是,我无法在我的生活中正确设置项目。需要确认我并没有失去理智...

'Call for available reports

Dim URLReporta As String
Dim JSONa As Object
Dim var As Object  
Set myrequesta = CreateObject("winhttp.winhttprequest.5.1")
URLReporta = ("https://secure.saashr.com:443/ta/rest/v1/reports?type=Saved&company%3shortname=" & Company)
myrequesta.Open "GET", URLReporta, False
myrequesta.setRequestHeader "Accept", "application/json"
myrequesta.setRequestHeader "Authentication", "Bearer " & Token
myrequesta.setRequestHeader "Content-Type", "application/json"
myrequesta.Send
Set JSONa = JsonConverter.ParseJson(myrequesta.responseText)
Set var = JSONa("SavedName")
Debug.Print var.Count

我在第Set var = JSONa("SavedName")行上收到错误:

  

运行时错误'424':需要对象

myrequesta.responseText值如下:

  

{“reports”:[{“SavedName”:“This Year”,“SettingId”:18959322},{“SavedName”:“Time Off Requests”,“SettingId”:18960210},{“SavedName”:“ Calc Hours Summary“,”SettingId“:18960209},{”SavedName“:”roster“,”SettingId“:18960211},{”SavedName“:”E / D / T“,”SettingId“:18823042},{” SavedName “:” TestZDR”, “SettingId”:18957188​​}]}

2 个答案:

答案 0 :(得分:2)

JsonConverter.ParseJson函数返回的结构不起作用。对于您的特定JSON,它包含3个级别: 根级对象只有一个属性reports,它包含第二级数组,后者又包含6个第三级对象,具有属性SavedNameSettingId。您正试图从根级对象获取第三级的对象属性值。

首先,您需要获取二级数组,然后遍历它的元素,包含对象,并检索该对象的SavedName属性值。这是一个例子:

'Call for available reports

Dim URLReporta As String
Dim JSONa As Object
Dim var As Object
Dim rep As Variant
Set myrequesta = CreateObject("winhttp.winhttprequest.5.1")
URLReporta = ("https://secure.saashr.com:443/ta/rest/v1/reports?type=Saved&company%3shortname=" & Company)
myrequesta.Open "GET", URLReporta, False
myrequesta.setRequestHeader "Accept", "application/json"
myrequesta.setRequestHeader "Authentication", "Bearer " & Token
myrequesta.setRequestHeader "Content-Type", "application/json"
myrequesta.Send
Set JSONa = JsonConverter.ParseJson(myrequesta.responseText) ' root level object
Set var = JSONa("reports") ' second level array
For Each rep In var ' third level objects
    Debug.Print rep("SavedName") ' property "SavedName" value of current third level object
Next

这是输出:

immediate

如果您只想获取报告数量,那么获取数组及其中的元素数量:

Debug.Print JSONa("reports").Count

答案 1 :(得分:1)

JSON对象可以被认为是词典的集合。因此,您必须遍历内部值(例如 SavedName )以检索整个字典对象(所有 SavedName 值)或索引位置的特定字符串值(一个 SavedName < / em> value):

Public Sub GetJSONRequest()
    Dim jsonStr As String

    jsonStr = "{" _
         & "     ""reports"": [{" _
         & "         ""SavedName"": ""This Year""," _
         & "         ""SettingId"": 18959322" _
         & "     }, {" _
         & "         ""SavedName"": ""Time Off Requests""," _
         & "         ""SettingId"": 18960210" _
         & "     }, {" _
         & "         ""SavedName"": ""Calc Hours Summary""," _
         & "         ""SettingId"": 18960209" _
         & "     }, {" _
         & "         ""SavedName"": ""roster""," _
         & "         ""SettingId"": 18960211" _
         & "     }, {" _
         & "         ""SavedName"": ""E/D/T""," _
         & "         ""SettingId"": 18823042" _
         & "     }, {" _
         & "         ""SavedName"": ""TestZDR""," _
         & "         ""SettingId"": 18957188" _
         & "  }]" _
         & "  }"

    Dim JSONa As Object, element As Object, e As Variant, i As Variant, var As Variant

    Set JSONa = ParseJson(jsonStr)

    ' DICTIONARY OBJECT
    Set element = CreateObject("Scripting.Dictionary")
    Set element = JSONa("reports")

    ' OUTER DICTIONARY
    For Each e In element
        ' INNER COLLECTION OF DICTIONARIES
        For Each i In e
            Debug.Print i & " " & e(i)
        Next i
    Next e            

    ' STRING VALUE OF FIRST SAVEDNAME VALUE
    var = JSONa("reports")(1)("SavedName")
    Debug.Print var

    Set element = Nothing
    Set JSONa = Nothing
End Sub

输出

SavedName This Year
SettingId 18959322
SavedName Time Off Requests
SettingId 18960210
SavedName Calc Hours Summary
SettingId 18960209
SavedName roster
SettingId 18960211
SavedName E/D/T
SettingId 18823042
SavedName TestZDR
SettingId 18957188
This Year