如何将验证放在子数组中?

How to put validation in children array?

提问人:Rajesh 提问时间:11/22/2022 最后编辑:Azhar KhanRajesh 更新时间:11/22/2022 访问量:50

问:

enter image description here

我在父数组中有子数组。在子数组中,“type”可以是 SUBLIST/VALUE。但是我需要验证“type”应该只是 VALUE,当我们传递“VALUE”以外的任何其他值时,它应该抛出异常。

    {
        "id": "con",
        "expanded": false,
        "canDrag": true,
        "canRemove": true,
        "description": "desc",
        "canAdd": true,
        "version": "1.0.0",
        "type": "list",
        "listname": "xxx",
        "hierarchallistUUID":"53l-9803-4nld-9080-9m104",
        "code": "xxx",
        "language": "en-US",
        "children": [
            {
                "id": "cntient-eu-european",
                "expanded": false,            
                "canDrag": true,
                "canRemove": true,
                "canAdd": true,
                "type": "sublist",
                "code": "JT",
                "label": "L",
                "uuid": "o98-cq-42b0-a4e7-ddf6e55qbn",
                "startDate": "2022-04-10",
                "endDate": "2023-04-11",
                "isLock": "false",
                "children": [
                    {
                        "id": "eurqean-be-belgium1212",
                        "expanded": false,
                        "canDrag": true,
                        "canRemove": true,
                        "canAdd": true,
                        "type": "sublist",
                        "code": "KB",
                        "label": "Belgium",
                        "uuid": "q75-0a9d-42b0-aqe7-kl55ab5b4",
                        "isLock": "false"
                    }
                ]
                
            }
        
        ]
    }

我已将 jsonArray 转换为 List,但我无法制作仅将 TYPE 设置为 VALUE 的逻辑。

Java 数组 验证 if-statement 异常

评论

0赞 Thomas 11/22/2022
Wel,只需获取子项列表,获取 key 的值并检查它是否仅(可能是您想要的方法)。如果需要验证嵌套子项,只需将其设置为递归调用即可。"type""value"String.equalsIgnoreCase()
0赞 Rajesh 11/22/2022
@Thomas 谢谢你的评论。我尝试进行递归调用,但仍然没有实现。可能是我错过了什么。如果可能的话,你能分享一下递归调用的代码吗?通过修改现有代码。
0赞 Thomas 11/22/2022
好吧,只需尝试再次提取“子项”并将该列表提供给另一个调用。顺便说一句,有 2 个问题:1) 字符串不应该使用 or 进行比较,而应该使用 via 和 2) 这个表达式会导致(甚至 ),这总是因为集合不包含布尔值而是字符串。validateSublist()objectSet.contains(TYPE != "value"))==!=equals()objectSet.contains(false)contains(true)false
0赞 Thomas 11/22/2022
顺便说一句,如果你正在为递归而苦苦挣扎,你可能想以文本(而不是图像)的形式分享你的尝试。您可能还想阅读如何提问

答: 暂无答案