|
Option Explicit
Public Function Get_JSON()
Dim ht As Object
Dim arrkeys() As Integer
Set ht = CreateObject("MSXML2.XMLHTTP")
Dim url As String
url = "https://api.apiopen.top/getImages"
ht.Open "GET", url, False
ht.send
Do While ht.readyState <> 4
DoEvents
Loop
Get_JSON = ht.responsetext
End Function
Public Sub JSON_Analysis1()
'初步设想通过递归遍历完成解析
Dim strJSON As String
strJSON = Get_JSON()
Call Recursion_JScriptTypeInfo(strJSON)
End Sub
Sub Recursion_JScriptTypeInfo(strJSON As String)
Dim objJSON As Object
Dim arrkeys() As String
Dim j As Integer, k
Dim strJSCode As String
Dim objkeys As Object
Dim intKeyLen As Integer
strJSCode = "function JSGetValue(jsonObj,strKey){return jsonObj[strKey];}"
strJSCode = strJSCode & " function JSGetKeys(jsonObj){var keys=new Array();for(var key in jsonObj){keys.push(key);}return keys;} "
With CreateObject("MSScriptControl.ScriptControl")
.Language = "javascript"
.AddCode strJSCode
Set objJSON = .eval("(" + strJSON + ")")
Set objkeys = .Run("JSGetKeys", objJSON) '(code, message, result)
intKeyLen = .Run("JSGetValue", objkeys, "length")
ReDim arrkeys(intKeyLen - 1)
j = 0
For Each k In objkeys
arrkeys(j) = k
j = j + 1
Next
For Each k In arrkeys
Debug.Print Trim(k)
If TypeName(.Run("JSGetValue", objJSON, k)) <> "JScriptTypeInfo" Then
Debug.Print Trim(.Run("JSGetValue", objJSON, k))
Else
strJSON = .Run("JSGetValue", objJSON, k)
Call Recursion_JScriptTypeInfo(strJSON)
End If
Next
End With
End Sub
|
|