You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LotusScript中遍历JSON对象someObject的属性并获取键值

在LotusScript中遍历JSON对象属性获取键和值

嘿,我来帮你搞定这个LotusScript遍历JSON属性的问题!你已经迈出了第一步,接下来我们把代码补全并修正细节,就能顺利获取每个属性的键和值了。

完整解决方案代码

首先,假设你使用的是Domino自带的NotesJSON相关类(这是Domino 10及以后版本支持的标准JSON处理方式),完整的实现代码如下:

' 定义你的JSON字符串
Dim jsonStr As String
jsonStr = |{ "someObject": { "Object_Name": "objName", "Alternative_Name" : "altName", "Alias_Name" : "alias" } }|

' 初始化JSON解析相关对象
Dim jsonReader As NotesJSONReader
Dim jsonRoot As NotesJSONObject
Dim targetObj As NotesJSONObject
Dim propertyKeys As Variant
Dim currentKey As String

Set jsonReader = New NotesJSONReader
Set jsonRoot = jsonReader.Parse(jsonStr)
' 获取目标对象someObject
Set targetObj = jsonRoot.GetItem("someObject")

' 获取该对象的所有属性键
propertyKeys = targetObj.GetKeys()

' 遍历每个键,获取对应的值
ForAll key In propertyKeys
    currentKey = key
    ' GetItemValue返回的是Variant数组,取第一个元素即可
    Dim currentValue As Variant
    currentValue = targetObj.GetItemValue(currentKey)(0)
    
    ' 这里可以根据需求处理键和值,比如打印输出
    Print "属性键: " & currentKey & ",对应值: " & currentValue
End ForAll

' 清理对象,释放资源
Set targetObj = Nothing
Set jsonRoot = Nothing
Set jsonReader = Nothing

关键细节说明

  • 获取属性键集合:NotesJSONObject没有直接的迭代器供ForAll直接遍历,需要先调用GetKeys()方法获取所有属性键的数组,再遍历这个数组。
  • 值的处理:GetItemValue()方法返回的是Variant类型的数组(因为JSON支持一个键对应多个值的场景),所以我们需要取数组的第一个元素(0)来获取单个属性值。
  • 对象清理:遍历完成后记得释放所有Notes对象,避免内存泄漏。

针对你现有代码的补充

你之前写的ForAll vResult In vResultData.Ite...可能是想直接迭代对象,但LotusScript的JSON对象不支持这种方式,换成先获取键数组再遍历的方式就能解决问题啦。

内容的提问来源于stack exchange,提问作者J.Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:13:56