vba - 在解析的JSON字符串中循环遍历嵌套数组

时间:2013-09-07 07:57:01

标签: arrays json access-vba

我使用的是Is There a JSON Parser for VB6 / VBA?中提到的解析器,但是我在获取嵌套数组的值时遇到了问题。 这是我的JSON字符串(来自Web服务的响应,我验证了它并且它是有效的JSON):

Dim stJsonResponse As String
stJsonResponse = {"stat":"ok","list":[{"url":["http://www.worldcat.org/oclc/69935068?referer=xid"],"publisher":"Tascabili Economici Newton","form":["BA"],"lang":"ita","city":"Milano","author":"Kahlil Gibran; org Tommaso Pisanti.","year":"1993","isbn":["9788879833172"],"title":"Aforismi sabbia e spuma","oclcnum":"69935068"]}]}

这是我到目前为止的代码:

Dim oJSON          As New JSON
Dim oJSONParsed    As Object

Set oJSONParsed = oJSON.parse(stJsonResponse)

For Each keyName In oJSONParsed.keys
    Select Case keyName
        Case "stat":
            Select Case oJSONParsed(keyName)
                Case "ok":
                    MsgBox "stat OK"
                Case "unknownId":
                    MsgBox "the request identifier looks like a valid ISBN number and unknown to xISBN service"
                Case "invalidId":
                    MsgBox "the request identifier is not a valid ISBN number"
                Case Else
                    'Case "unknownField": 'the request field is not supported
                    'Case "unknownFormat": 'the request format is not supported
                    'Case "unknownLibrary": 'the request library is not supported
                    'Case "unknownMethod": 'the request method is not supported
                    'Case "overlimit": 'the request is throttled, only header is returned
                    'Case "invalidAffiliateId": 'invalid affiliate id
                    'Case "invalidHash": 'invalid hash (subscription only)
                    'Case "invalidToken": 'invalid access token (subscription only)
                    MsgBox "Errore irreversiblie, Case interno su 'stat'"
            End Select
        Case "list":
            'Here I am stuck, I tried several options but I do not know how to proceed
            'Here latest attempt
            Dim temp As String
            temp = ""
            Dim colJSONArray   As Collection
            Set colJSONArray = oJSONParsed.Item("list")
            For Each key In colJSONArray
                '-------> How do I get the values here? <-------
                temp = temp + "Key: " + key + " Value: " + colJSONArray.Item("key") + vbNewLine
            Next
            MsgBox temp
        Case Else
            MsgBox "Errore irreversiblie"
    End Select
Next

代码colJSONArray.Item("key")给出了错误Invalid procedure call or argument
代码print colJSONArray.Count为我提供1
任何帮助表示赞赏。

编辑:愚蠢的我......无法在“”中写入密钥! 'colJSONArray.Item(1)'工作并给我一个字典...我会从这里移动。

1 个答案:

答案 0 :(得分:0)

如果colJSONArray.Item(“key”)是集合你需要得到这个集合的第一个元素。

...
dim tmp
For Each key In colJSONArray
     tmp = colJSONArray.Item("key")
     temp = temp + "Key: " + key + " Value: " + tmp(Lbound(tmp)) + vbNewLine
next key
....