如何在Groovy JSON xpath中获取所有索引值

szqfcxe2  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(109)

请找到附加的Groovy代码,我使用它从响应主体中获取特定的文件。
查询1:
当I使用正确的索引值时,它检索结果,如如果数据.RenewalDetails[o],给予输出值为1,如果数据.RenewalDetails[1],则输出值为2。
但在我的真实的情况下,我永远不会知道响应中的块数,所以我想得到所有满足条件的值,我尝试了data.RenewalDetails[*],但它不工作。你能帮忙吗?
疑问二:
除了上述条件外,我还想再添加一个筛选器,其中“FamilyCode”:在Itemdetails中有“PREMIUM”,你能帮忙吗?

def BoundId = new groovy.json.JsonSlurper().parseText('{"data":{"RenewalDetails":[{"ExpiryDetails":{"duration":"xxxxx","destination":"LHR","from":"AUH","value":2,"segments":[{"valudeid":"xxx-xx6262-xxxyyy-1111-11-11-1111"}]},"Itemdetails":[{"BoundId":"Value1","isexpired":true,"FamilyCode":"PREMIUM","availabilityDetails":[{"travelID":"AAA-AB1234-AAABBB-2022-11-10-1111","quota":"X","scale":"XXX","class":"X"}]}]},{"ExpiryDetails":{"duration":"xxxxx","destination":"LHR","from":"AUH","value":2,"segments":[{"valudeid":"xxx-xx6262-xxxyyy-1111-11-11-1111"}]},"Itemdetails":[{"BoundId":"Value2","isexpired":true,"FamilyCode":"PREMIUM","availabilityDetails":[{"travelID":"AAA-AB1234-AAABBB-2022-11-10-1111","quota":"X","scale":"XXX","class":"X"}]}]}]},"warnings":[{"code":"xxxx","detail":"xxxxxxxx","title":"xxxxxxxx"}]}')
        .data.RenewalDetails[0].Itemdetails.find { itemDetail ->
    itemDetail.availabilityDetails[0].travelID.length() == 33
}?.BoundId

println "Hello " + BoundId
bsxbgnwa

bsxbgnwa1#

大概是这样的:

def txt = '''\
  {
    "data": {
        "RenewalDetails": [
            {
                "ExpiryDetails": {
                    "duration": "xxxxx",
                    "destination": "LHR",
                    "from": "AUH",
                    "value": 2,
                    "segments": [
                        {
                            "valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
                        }
                    ]
                },
                "Itemdetails": [
                    {
                        "BoundId": "Value1",
                        "isexpired": true,
                        "FamilyCode": "PREMIUM",
                        "availabilityDetails": [
                            {
                                "travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
                                "quota": "X",
                                "scale": "XXX",
                                "class": "X"
                            }
                        ]
                    }
                ]
            },
            {
                "ExpiryDetails": {
                    "duration": "xxxxx",
                    "destination": "LHR",
                    "from": "AUH",
                    "value": 2,
                    "segments": [
                        {
                            "valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
                        }
                    ]
                },
                "Itemdetails": [
                    {
                        "BoundId": "Value2",
                        "isexpired": true,
                        "FamilyCode": "PREMIUM",
                        "availabilityDetails": [
                            {
                                "travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
                                "quota": "X",
                                "scale": "XXX",
                                "class": "X"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "warnings": [
        {
            "code": "xxxx",
            "detail": "xxxxxxxx",
            "title": "xxxxxxxx"
        }
    ]
}'''

def json = new groovy.json.JsonSlurper().parseText txt

List<String> BoundIds = json.data.RenewalDetails.Itemdetails*.find { itemDetail ->
  itemDetail.availabilityDetails[0].travelID.size() == 33 && itemDetail.FamilyCode == 'PREMIUM'
}?.BoundId

assert BoundIds.toString() == '[Value1, Value2]'

请注意,您将以列表的形式获得BoundId

wgx48brx

wgx48brx2#

1.如果您像这样修改代码:

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData()

您可以按如下方式访问返回的项目数:

def size = json.data.RenewalDetails.size()

因为RenewalDetails表示List
1.只需使用Groovy的&&操作符添加任意多个查询即可:

find { itemDetail ->
     itemDetail.availabilityDetails[0].travelID.length() == 33 &&
     itemDetail.FamilyCode.equals('PREMIUM')
 }

更多信息:

相关问题