我正在建立一个VBA应用程序,使用从网络上刮来的资源创建和修改Wordpress网站页面。Wordpress的API会返回一个JSON文件,但在VBA中没有对JSON进行解析的本地支持,所以我从GitHub上导入了VBA-JSON。这里是子程序。
Sub Wordpress()
' Wordpress API Test
Dim wpResp As Variant
Dim sourceSheet As String
Dim resourceURL As String
sourceSheet = "Resources"
resourceURL = Sheets(sourceSheet).Cells(6, 1)
wpResp = getJSON(resourceURL + "/wp-json/wp/v2/posts")
End Sub
以及它所调用的函数。
Function getJSON(link) As Object
Dim response As String
Dim json As Object
On Error GoTo recovery
Dim retryCount As Integer
retryCount = 0
Dim web As MSXML2.XMLHTTP60
Set web = New MSXML2.XMLHTTP60
the_start:
web.Open "GET", link, False, UserName, pw
web.setRequestHeader "Content-type", "application/json"
web.send
response = web.responseText
While web.readyState <> 4
DoEvents
On Error GoTo 0
Debug.Print link
Debug.Print web.Status; "XMLHTTP status "; web.statusText; " at "; Time
Set json = JsonConverter.ParseJson(response)
'getJSON = json ' this line produces Object variable or With block variable not set error but I can deal with it later
Exit Function
recovery:
retryCount = retryCount + 1
Debug.Print "Error number: " & Err.Number & " " & Err.Description & " Retry " & retryCount
Application.StatusBar = "Error number: " & Err.Number & " " & Err.Description & " Retry " & retryCount
If retryCount < 4 Then GoTo the_start Else Exit Function
End Function
这段代码返回一个有1个项目的对象/集合,其中包含一个有24个项目的变量/对象/字典,但我不知道如何访问这些项目。 下面是一个截图。
如果我使用即时窗口查询?json.count,我得到正确的结果 "1",但在网上研究了大约6个小时,并尝试了我能找到的许多变体,我仍然被困于如何访问其他24个。
这里是JSON文件。
[{"id":1,"date":"2018-06-22T18:13:00","date_gmt":"2018-06-22T22:13:00","guid":{"rendered":"http:\/\/mytestsite.org\/?p=1"},"modified":"2018-06-22T18:13:00","modified_gmt":"2018-06-22T22:13:00","slug":"hello-world","status":"publish","type":"post","link":"http:\/\/mytestsite.org\/hello-world\/","title":{"rendered":"Blog Post Title"},"content":{"rendered":"<p>What goes into a blog post? Helpful, industry-specific content that: 1) gives readers a useful takeaway, and 2) shows you’re an industry expert. <\/p>\n<p>Use your company’s blog posts to opine on current industry topics, humanize your company, and show how your products and services can help people.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What goes into a blog post? Helpful, industry-specific content that: 1) gives readers a useful takeaway, and 2) shows you’re…<\/p>\n","protected":false},"author":1,"featured_media":212,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/posts\/1"}],"collection":[{"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/media\/212"}],"wp:attachment":[{"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mytestsite.org\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]
在一天结束时,我希望能够把从几个互联网来源提取和整理的几百页WP内容旋转起来,并使用这个应用程序保持它们的更新。只要我们不超出VBA的范围,对这里的问题提出进一步的建议也会很有用。
JsonConverter 返回一个VBA.Collections Scripting.Dictionaries和Values的集合。为了理解输出,你必须测试所有返回值的
。
TypeName
真正的问题是 "如何浏览一个
对象(或任何未知的对象)并访问其中的值。
json
使用
和OP帖子中的
对象,我将尝试描述思维过程(以必读书籍的风格: 《小谋士 》)。
Immediate Window
json
' What is json? ?TypeName(JSON) Collection 'json is a collection 'How big is JSON ?JSON.Count 'JSON is a collection of 1 Item 'What is Type that Item? ?TypeName(JSON(1)) Dictionary 'JSON(1) is a Dictionary 'What is the first key in the JSON(1) Dictionary? ?JSON(1).Keys()(0) 'The first key in the JSON(1) Dictionary is "id" 'What is the Type of the value of "id"? ?TypeName(JSON(1)("id")) Double 'JSON(1)("id") is a number 'What is its value ?JSON(1)("id")
当然,考虑到这个JSON对象中的嵌套量,这个过程会变得很繁琐。
JSON(1)("_links")("curies")(1)("templated")
集合|字典|字典|集合|布尔值
所以我想最好的办法是写一个函数,将所有的访问器打印到
,然后从那里开始。Immediate Window
PrintJSONAccessors:Sub