如何在 VBA Excel 中以良好格式显示我的 JSON 对象

Rib*_*ugo 1 excel vba json

我知道当 Json 是“简单”时(当它只是字符串时)在单元格 Excel 中显示我的 Parse Json,但现在我有字符串、对象和数组,我有点迷路了..我的 json 如下:

[
    {
        "name": null,
        "type": null,
        "actions": [],
        "screen": null,
        "container": null,
        "sysid": 5,
        "uftitem": null
    },
    {
        "name": null,
        "type": null,
        "actions": [],
        "screen": null,
        "container": null,
        "sysid": 6,
        "uftitem": null
    },
    {
        "name": "UTProject5",
        "type": "type",
        "actions": [
            {
                "name": "UTProject",
                "description": "UTProject",
                "pattern": "UTProject",
                "isCheck": true,
                "sysid": 1,
                "uftaction": {
                    "sysid_uftAction": 2,
                    "code": "code uft",
                    "maxTime": 10,
                    "nbCycle": 20
                }
            },
            {
                "name": "UTProject2",
                "description": "UTProject",
                "pattern": "UTProject",
                "isCheck": true,
                "sysid": 3,
                "uftaction": {
                    "sysid_uftAction": 4,
                    "code": "code uft",
                    "maxTime": 10,
                    "nbCycle": 20
                }
            }
        ],
        "screen": {
            "name": null,
            "type": null,
            "actions": [],
            "screen": null,
            "container": null,
            "sysid": 5,
            "uftitem": null
        },
        "container": {
            "name": null,
            "type": null,
            "actions": [],
            "screen": null,
            "container": null,
            "sysid": 6,
            "uftitem": null
        },
        "sysid": 7,
        "uftitem": {
            "code": "code",
            "parentCode": "tooooz",
            "sysid": 8
        }
    },
    {
        "name": "UTProject6",
        "type": "type",
        "actions": [
            {
                "name": "UTProject",
                "description": "UTProject",
                "pattern": "UTProject",
                "isCheck": true,
                "sysid": 1,
                "uftaction": {
                    "sysid_uftAction": 2,
                    "code": "code uft",
                    "maxTime": 10,
                    "nbCycle": 20
                }
            },
            {
                "name": "UTProject2",
                "description": "UTProject",
                "pattern": "UTProject",
                "isCheck": true,
                "sysid": 3,
                "uftaction": {
                    "sysid_uftAction": 4,
                    "code": "code uft",
                    "maxTime": 10,
                    "nbCycle": 20
                }
            }
        ],
        "screen": {
            "name": null,
            "type": null,
            "actions": [],
            "screen": null,
            "container": null,
            "sysid": 5,
            "uftitem": null
        },
        "container": {
            "name": null,
            "type": null,
            "actions": [],
            "screen": null,
            "container": null,
            "sysid": 6,
            "uftitem": null
        },
        "sysid": 9,
        "uftitem": {
            "code": null,
            "parentCode": null,
            "sysid": 10
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

我想访问我想要的内容并将其显示在单元格中,但我不知道在数组和对象中的访问权限。

谢谢大家!

QHa*_*arr 5

一般的:

您可以使用以下使用JSON 转换器的代码清空整个内容:


笔记:

我正在从工作表中读取 JSON 字符串并通过JSONConverter. 初始对象是一个集合。我使用TypeNamefunction*循环该集合和每个嵌套级别,以确定每个级别存储的对象。然后我Select Case用来适当地处理这些对象。

更有效的是设计一个可重用的类来处理这个问题。我已经在 SO 上看到了其他一些问题,这是在哪里完成的。

* VarType实际上更健壮


示例 JSON

示例 JSON


示例代码输出到立即窗口:

您可以通过将Debug.Print语句替换为对工作表范围的赋值来选择写入单元格的方式。

示例代码输出


VBA:

Option Explicit
Public Sub GetInfoFromSheet()
    Dim jsonStr As String
    jsonStr = [A1]                               '<== read in from sheet
    Dim json As Object
    Set json = JsonConverter.ParseJson(jsonStr)

    Dim i As Long, j As Long, key As Variant
    For i = 1 To json.Count
        For Each key In json(i).keys
            Select Case key
            Case "name", "type"
                Debug.Print key & " " & json(i)(key)
            Case Else
                Select Case TypeName(json(i)(key))
                Case "Dictionary"
                    Dim key2 As Variant
                    For Each key2 In json(i)(key)
                        Select Case TypeName(json(i)(key)(key2))
                        Case "Collection"
                            Dim k As Long
                            For k = 1 To json(i)(key)(key2).Count
                                Debug.Print key & " " & key2 & " " & json(i)(key)(key2)(k)
                            Next k
                        Case Else
                            Debug.Print key & " " & key2 & " " & json(i)(key)(key2)
                        End Select
                    Next key2
                Case "Collection"
                    For j = 1 To json(i)(key).Count '<== "actions"
                        Dim key3 As Variant
                        For Each key3 In json(i)(key)(j).keys
                            Select Case TypeName(json(i)(key)(j)(key3))
                            Case "String", "Boolean", "Double"
                                Debug.Print key & " " & key3 & " " & json(i)(key)(j)(key3)
                            Case Else
                                Dim key4 As Variant
                                For Each key4 In json(i)(key)(j)(key3).keys
                                    Debug.Print key & " " & key3 & " " & key4 & " " & json(i)(key)(j)(key3)(key4)
                                Next key4
                            End Select
                        Next key3
                    Next j
                Case Else
                    Debug.Print key & " " & json(i)(key)
                End Select
            End Select
        Next key
    Next i
End Sub
Run Code Online (Sandbox Code Playgroud)

tl;博士; 教程点:

所以上面的内容可能有点冗长,因为它在没有太多解释的情况下得到了一切。下面,我们将更详细地了解如何定位一些 JSON 并通过关联的 VBA 进行“交谈”

为此,您可以使用在线 JSON 解析器来JSON更清楚地查看您的结构。我将您的JSON字符串发布到Json Parser Online 中,然后检查了String/parseJS eval; 中的结构。左侧部分。还有其他可用的工具。

首先要注意的是开始"["。您可以在下面看到的第一个。

开始

这表示Collection对象,即JSON使用JsonConverter. 其他所有内容都嵌套在这个左"["括号和最后的右括号之间。

接下来要注意的是,这是一个字典的集合,因此在其中形成“组”的所有内容都是字典。

字典

看到"{"表示字典开头的了吗?

字典有键"name","type","actions"等。

初步观察是,很多这些信息是空的,即。null. 我们可以通过IsNull测试忽略这些(我选择根据"name"字段执行此操作):

If Not IsNull(json(i)("name")) 
Run Code Online (Sandbox Code Playgroud)

我们还可以看到"actions",在"name"不是 的字典中null,包含另一个字典集合。你看,我们有前面描述的"["后面跟着的"{"

字典集

我们可以看到每个内部字典都有"name", "description"etc 的键。我们还可以看到它们的值是不同的数据类型。

"actions"JSON结构中观察,您可以看到这些是(使用示例字典):

  1. 细绳 "name":"UTProject"
  2. 细绳 "description":"UTProject"
  3. 细绳 ”pattern":"UTProject"
  4. 布尔值 "isCheck":true
  5. 双倍的 "sysid":1
  6. 字典 "uftaction" 'keys of ==> "sysid_uftAction":2,"code":"code uft","maxTime":10,"nbCycle":20

因此,我们可以使用Select Case通过测试来处理数据类型TypeName

对于原始布尔值、字符串和双精度数据类型,我们可以简单地使用键打印它们,例如

json(i)("actions")(j)("isCheck")
Run Code Online (Sandbox Code Playgroud)

这将是Trueor的布尔结果,False. i并且j是外部和内部集合循环中当前位置的索引。

对于字典"uftaction",我们可以遍历它的键:

For Each key2 In json(i)("actions")(j)(key).keys 
    Debug.Print "actions " & key & " " & key2 & " " & json(i)("actions")(j)(key)(key2)
Next key2
Run Code Online (Sandbox Code Playgroud)

您当然可以使用密钥的名称进行访问,而无需在最后对密钥进行循环,例如:

json(i)("actions")(j)(key)("maxTime")
Run Code Online (Sandbox Code Playgroud)

在整个过程中,您可以通过索引而不是循环访问特定位置,这样iandj将直接替换为数值。而key, key2等可以通过任何给定的实际的文本字符串替换

希望这给了你更多的见识。

VBA:

Option Explicit
Public Sub GetInfoFromJSON()
    Dim jsonStr As String
    jsonStr = [A1]                               '<== read in from sheet
    Dim json As Object, i As Long
    Set json = JsonConverter.ParseJson(jsonStr) '<==This is a collection verified by Debug.Print TypeName(JSON)
    For i = 1 To json.Count
        If Not IsNull(json(i)("name")) Then
            'ignore the null names which have sys id only populated
            Debug.Print "name" & " " & json(i)("name")
            Debug.Print "type" & " " & json(i)("type")
            Dim j As Long
            For j = 1 To json(i)("actions").Count 'actions are a collection of dictionaries
                Dim key As Variant
                For Each key In json(i)("actions")(j).keys 'dictionary
                    'observing actions in the JSON structure you can see there are:
                    '                    String  "name":"UTProject"
                    'String "description":"UTProject",
                    'String "pattern":"UTProject",
                    'Boolean "isCheck":true,
                    'Double "sysid":1,
                    'Dictionary "uftaction" '==> "sysid_uftAction":2,"code":"code uft","maxTime":10,"nbCycle":20
                    'So we can use Select Case to handle the data type by testing with TypeName
                    Select Case TypeName(json(i)("actions")(j)(key))
                    Case "String", "Boolean", "Double" '<==good to go nothing extra needed
                        Debug.Print "actions " & key & " " & json(i)("actions")(j)(key)
                    Case Else                    ' we are dealing with uftaction which we know is a dictionary
                        Dim key2 As Variant
                        For Each key2 In json(i)("actions")(j)(key).keys '<==We know now we are looping the uftaction dictionary which has keys "sysid_uftAction","code","maxTime","nbCycle"
                            Debug.Print "actions " & key & " " & key2 & " " & json(i)("actions")(j)(key)(key2)
                        Next key2
                    End Select
                Next key
            Next j
        End If
    Next i
End Sub
Run Code Online (Sandbox Code Playgroud)