您可以编写一个递归子程序来测试每个结构在 JSON 中的内容并进行适当处理。此外,您需要在开头移动尾随“,”的位置,以便它实际上分隔 JSON 中的项目。
所以在 A1 和 A2 我有以下内容:
[{"oeange":"good"},{"banana":{"color":"yellow"}},{"cat":"grumpy"}]
[{"oeange":"good"},{"banana":null},{"cat":"grumpy"}]
VBA:
Option Explicit
Public r As Long
Public Sub GetInfoFromSheet()
Dim json As Object, jsonSource(), i As Long, ws As Worksheet, arr() As String
Set ws = ThisWorkbook.Worksheets("Sheet1")
jsonSource = Application.Transpose(ws.Range("A1:A2").Value)
For i = LBound(jsonSource) To UBound(jsonSource)
Set json = JsonConverter.ParseJson(jsonSource(i))
EmptyJSON json
Next i
End Sub
Public Sub EmptyJSON(ByVal json As Object)
Dim key As Variant, item As Object
Select Case TypeName(json)
Case "Dictionary"
For Each key In json
Select Case TypeName(json(key))
Case "Dictionary"
EmptyJSON json(key)
Case Else
r = r + 1
With ThisWorkbook.Worksheets("Sheet1")
.Cells(r, 2) = key
.Cells(r, 3) = json(key)
End With
End Select
Next
Case "Collection"
For Each item In json
EmptyJSON item
Next
End Select
End Sub
输出: