VBA-JSON项目解析:处理多层嵌套JSON数据中的NULL值问题
VBA-JSON 项目地址: https://gitcode.com/gh_mirrors/vb/VBA-JSON
引言
在VBA开发中处理JSON数据时,经常会遇到多层嵌套结构中某些字段为NULL的情况。本文将深入探讨如何利用VBA-JSON库有效处理这类复杂数据结构,特别是针对包含NULL值的嵌套JSON对象。
VBA-JSON的基本映射原理
VBA-JSON库在处理JSON数据时遵循特定的映射规则:
- JSON数组会被转换为VBA的Collection对象
- JSON对象会被转换为VBA的Scripting.Dictionary对象
- JSON中的null值在VBA中表现为Nothing
理解这种映射关系对于正确处理JSON数据至关重要。开发者需要明确区分VBA中的Collection和Dictionary对象,因为它们具有完全不同的接口和方法。
处理NULL值的核心挑战
当JSON数据中存在多层嵌套且某些中间层为NULL时,直接访问深层属性会导致运行时错误。例如:
{
"timecard_time_type": null,
"custom_fields": {
"custom_field_123": {
"value": [
{
"id": 123,
"label": "示例标签"
}
]
}
}
}
在这个结构中,如果直接访问timecard_time_type.time_type
或custom_fields.custom_field_123.value[0].id
而不进行NULL检查,程序就会崩溃。
解决方案:构建安全的访问函数
基础NULL检查函数
我们可以创建一个通用的Clean函数来处理简单的NULL值情况:
Function Clean(Jdict As Variant, Jkey As String)
Clean = ""
If TypeName(Jdict) <> "Dictionary" Then Exit Function
If Not Jdict.Exists(Jkey) Then Exit Function
If IsNull(Jdict(Jkey)) Then Exit Function
Clean = Jdict(Jkey)
End Function
这个函数会:
- 检查输入是否为Dictionary对象
- 检查指定的键是否存在
- 检查值是否为NULL
- 只有所有检查通过才返回值,否则返回空字符串
处理复杂嵌套结构的增强函数
对于更复杂的多层嵌套结构,我们需要一个能返回对象的函数:
Function CleanObj(Jdict As Variant, Jkey As String, Expect As String) As Object
Set CleanObj = Nothing
If TypeName(Jdict) <> "Dictionary" Then Exit Function
If Not Jdict.Exists(Jkey) Then Exit Function
If IsNull(Jdict(Jkey)) Then Exit Function
If TypeName(Jdict(Jkey)) <> Expect Then Exit Function
Set CleanObj = Jdict(Jkey)
End Function
这个函数增加了对预期对象类型的检查,确保返回的对象符合我们的期望。
实际应用示例
处理设备资源数据
假设我们需要处理设备资源数据,其中包含复杂的custom_fields结构:
Sub ProcessResourceData(JSON As Collection)
Dim CurrentRow As Range
Dim cfdata As New Dictionary
Set CurrentRow = ThisWorkbook.Sheets("DATArsc").Rows(2)
For Each Item In JSON
With CurrentRow
.Cells(1, 1) = Item("id")
.Cells(1, 2) = Clean(Item("equipment"), "project_id")
.Cells(1, 3) = ""
.Cells(1, 4) = Clean(Item("cost_code"), "long_name")
' 处理复杂的custom_fields结构
ProcessCustomFields Item, "custom_field_598134325561114", cfdata
.Cells(1, 6) = cfdata.Item("id")
.Cells(1, 7) = cfdata.Item("label")
End With
Set CurrentRow = CurrentRow.Offset(1, 0)
Next
End Sub
Sub ProcessCustomFields(Item, Fkey As String, ByRef cfdata As Dictionary)
Dim cfsdict As Dictionary, cfdict As Dictionary, cfvdict As Dictionary
Dim cfvcoll As Collection, entry
' 初始化返回字典
cfdata.RemoveAll
cfdata.Add "id", ""
cfdata.Add "label", ""
' 逐层安全访问
Set cfsdict = CleanObj(Item, "custom_fields", "Dictionary")
If cfsdict Is Nothing Then Exit Sub
Set cfdict = CleanObj(cfsdict, Fkey, "Dictionary")
If cfdict Is Nothing Then Exit Sub
Set cfvcoll = CleanObj(cfdict, "value", "Collection")
If cfvcoll Is Nothing Then Exit Sub
' 获取第一个有效条目
Set cfvdict = Nothing
For Each entry In cfvcoll
If TypeName(entry) = "Dictionary" Then
Set cfvdict = entry
Exit For
End If
Next
' 填充返回数据
cfdata.Item("id") = Clean(cfvdict, "id")
cfdata.Item("label") = Clean(cfvdict, "label")
End Sub
最佳实践建议
- 始终进行类型检查:在使用任何JSON数据前,先检查其类型是否符合预期
- 分层处理复杂结构:对于多层嵌套的数据,逐层处理并检查每层的有效性
- 使用辅助函数:创建通用的NULL检查和类型转换函数提高代码复用性
- 合理初始化默认值:为可能为NULL的字段设置合理的默认值
- 错误处理:添加适当的错误处理机制,防止意外情况导致程序崩溃
总结
处理VBA中的多层嵌套JSON数据需要特别注意NULL值情况。通过构建安全的访问函数和采用分层处理策略,可以有效地从复杂JSON结构中提取所需数据,同时保证代码的健壮性。本文介绍的技术不仅适用于VBA-JSON项目,其核心思想也可以应用于其他JSON处理场景。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考