[英]vba - Loop through nested array in parsed JSON string
我正在使用中提到的解析器: 是否有適用於 VB6/VBA 的 JSON 解析器? 但我在獲取嵌套數組的值時遇到問題。 這是我擁有的 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)' 工作並給我一個字典......我將從這里搬走。
如果 colJSONArray.Item("key") 是 collection 您需要獲取此集合的第一個元素。
...
dim tmp
For Each key In colJSONArray
tmp = colJSONArray.Item("key")
temp = temp + "Key: " + key + " Value: " + tmp(Lbound(tmp)) + vbNewLine
next key
....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.