我使用groovy脚本来解析值。我想保存来自1 API的响应,并且需要将该响应作为其他API的请求传递。 例如:
{"LoginResult":{"AccessToken":"abc…","SessionToken":"abcd…."},"SessionID":null,"UserDetails":{"FirstName":"","LastName":"","SalesPersonCode":"","SalesPersonDiscount":0.0,"IsServiceAdvisor":true,"EmployeeNo":""},"Companies":[{"Name":" Test ","Locations":[{"Code":"L12","Name":"Branch "}],"Makes":[{"Code":"","Name":""}],"Roles":[{"Name":""}],"Address":"","Address2":"No. 88 Century Avenue","BankAcctNo":"","BankBranchNo":"","BankName":"","City":"","Email":"","FaxNo":"","IBAN":"","PhoneNo":"","PostCode":"","SwiftBIC":"","VatRegNo":"","Web":"","DisplayName":"","DisplayName2":""}]}
在这个例子中,我想保存Location值和Make代码,需要传递下一个API的请求。
答案 0 :(得分:1)
你走了:
import groovy.json.*
def t = """{"LoginResult":{"AccessToken":"abc…","SessionToken":"abcd…."},"SessionID":null,"UserDetails":{"FirstName":"","LastName":"","SalesPersonCode":"","SalesPersonDiscount":0.0,"IsServiceAdvisor":true,"EmployeeNo":""},"Companies":[{"Name":" Test ","Locations":[{"Code":"L12","Name":"Branch "}],"Makes":[{"Code":"","Name":""}],"Roles":[{"Name":""}],"Address":"","Address2":"No. 88 Century Avenue","BankAcctNo":"","BankBranchNo":"","BankName":"","City":"","Email":"","FaxNo":"","IBAN":"","PhoneNo":"","PostCode":"","SwiftBIC":"","VatRegNo":"","Web":"","DisplayName":"","DisplayName2":""}]}
"""
def j = new JsonSlurper().parseText(t)
j.Companies.Locations.flatten().each { println it}
assert j.Companies.Locations.flatten().find { it.Code=='L12' && it.Name=='Branch ' }