groovy 如何检查嵌套数据结构中是否存在元素

6yoyoihd  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(226)
test_list = [Record(topic = nr-dev-01, partition = 0, offset = 10, serialized key size = 3, serialized value size = 873, headers = RecordHeaders(headers = [], isReadOnly = false), key = oregon_ht01, value = {"push_status": "SUCCESS", "push_summary": {"_index": "date_io_kl", "_package": "_zip", "id": "tUz1", "version": 1, "found": "true", "_source": {"Manual": {"Preamble": {"Product_name": {"@value": "Flight manual 101"}, "Version": {"@value": "1.0.0"}, "License_info": {"@value": "LICENSED COPY - February 03 2023"}}}}}}]

我正在尝试查找'key'是否作为test_list的一部分存在,如果是,则获取其值,理想情况下(oregon_ht01)
但它不能说
groovy tst.groovy

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
tst.groovy: 1: expecting ')', found 'key' @ line 1, column 133.
   e = 1684410534479, serialized key size =

代码段

def keyToFind = 'oregon_ht01'

if (test_list.contains(keyToFind)) {
    println("Key $keyToFind found in messages")
} else {
    println("Key $keyToFind not found in messages")
}
oyxsuwqo

oyxsuwqo1#

假设test_list只包含Record类型的对象,并且Record的所有属性都是公共的,代码如下所示:

if( test_list.any{ it.key == keyToFind } ) {}

相关问题