如何在lotusscript(ls.snapps.JSONReader)中迭代JSON对象中的键名

时间:2017-05-01 17:35:58

标签: json lotusscript

相关问题: Trying to get lotusscript json reader

如果我们之前在JSon对象中不知道密钥,我们如何迭代密钥名称?如果是非数组。

{   "colorsArray":{
        "red":"#f00",
        "green":"#0f0",
        "blue":"#00f",
        "cyan":"#0ff",
        "magenta":"#f0f",
        "yellow":"#ff0",
        "black":"#000" ,
        .....
    }
}

如果vResult.Key存在吗?

1 个答案:

答案 0 :(得分:0)

如果你想要遍历' colorsArray'而你不知道钥匙然后你可以做这样的事情:

Dim jsonReader As JSONReader
Dim vResults As Variant
Dim vPieces As Variant, vResultData  As variant
Dim sJSON As String
sJson= |{   "colorsArray":[{
    "red":"#f00",
    "green":"#0f0",
    "blue":"#00f",
    "cyan":"#0ff",
    "magenta":"#f0f",
    "yellow":"#ff0",
    "black":"#000"
}
]}|

Set jsonReader = New JSONReader
Set vResults = jsonReader.parse(sJson)

Set vResultData = vResults.GetItemValue("colorsArray")      
ForAll vResult In vResultData.Items
    Forall tmp In vResult.Items         
        Print "Key: " +  ListTag(tmp) + " Value: " + tmp    
    End ForAll
End ForAll

如果你想检查一些密钥是否存在,那么写一个函数:

Function isKeyAvailable(items As Variant, keyName As String) As Boolean
    If IsElement ( items(keyName) ) Then
        isKeyAvailable = true
    Else
        isKeyAvailable = false
    End If
End Function

然后你可以检查密钥是否存在:

...
Set vResultData = vResults.GetItemValue("colorsArray")      
ForAll vResult In vResultData.Items
    If isKeyAvailable(vResult.Items, "green") Then
        Print "Exist"
    Else
        Print "Does not exist"
    End If
End ForAll
...

如果您不熟悉Lotusscript中的列表,此链接将为您提供帮助:

https://www.ibm.com/support/knowledgecenter/en/SSVRGU_9.0.1/basic/LSAZ_WORKING_WITH_LISTS.html