我正在使用Groovy RESTClient进行一些API自动化。
我遇到的问题是当我进行GET调用时,我得到的结果是缺少我期望的所有引号和格式。这使得解析结果变得很困难。
示例:
我期望什么(以及我从API获得的内容)......
{
"results": [{
"licenseType": "mobileAppLicensesWithDevice",
"name": "Lead Retrieval - Device Rental & App license",
"owner": {
"id": "a705c768-ee33-491d-a993-4dd7bc61228b",
"entityType": "exhibitor"
},
"termId": "630493a4-4a70-4f4f-afaf-31610c14c181",
"id": "c215affe-ed0f-4014-8ba8-53f9df97942a",
"readableId": "77umkh20kq3",
"accessCode": "0w7zh6t",
"updatedAt": "2016-09-22T17:02:06.911Z",
"createdAt": "2016-09-22T17:02:06.911Z"
}, {
"licenseType": "mobileAppLicensesWithDevice",
"name": "Lead Retrieval - Device Rental & App license",
"owner": {
"id": "a705c768-ee33-491d-a993-4dd7bc61228b",
"entityType": "exhibitor"
},
"termId": "630493a4-4a70-4f4f-afaf-31610c14c181",
"id": "4249aedb-934f-4db1-89d6-6f7f10152bf5",
"readableId": "fyzv5ay7tfs",
"accessCode": "ray0pwb",
"updatedAt": "2016-09-22T17:02:06.911Z",
"createdAt": "2016-09-22T17:02:06.912Z"
}, {
"licenseType": "mobileAppLicensesWithDevice",
"name": "Lead Retrieval - Device Rental & App license",
"owner": {
"id": "a705c768-ee33-491d-a993-4dd7bc61228b",
"entityType": "exhibitor"
},
"termId": "630493a4-4a70-4f4f-afaf-31610c14c181",
"id": "ea6de933-0043-435c-ad4e-32b81018ec05",
"readableId": "ytby08d586x",
"accessCode": "3sv1lj6",
"updatedAt": "2016-09-22T17:02:06.912Z",
"createdAt": "2016-09-22T17:02:06.912Z"
}],
"etags": [{}, {}, {}],
"total": 3
}
以下是我在使用RESTClient时得到的内容......
{
etags = [{}, {}, {}], results = [{
accessCode = wvnc16i,
createdAt = 2016 - 09 - 23 T15: 08: 20.673 Z,
id = 06 f7bb76 - cc0c - 450 d - a4af - fcf3392fbd1b,
licenseType = mobileAppLicensesWithDevice,
name = Lead Retrieval - Device Rental & App license,
owner = {
entityType = exhibitor,
id = adc7a8e5 - b137 - 40 c6 - 8765 - deb3ce0b8b3f
},
readableId = r52jvlr0ok7,
termId = 630493 a4 - 4 a70 - 4 f4f - afaf - 31610 c14c181,
updatedAt = 2016 - 09 - 23 T15: 08: 20.673 Z
}, {
accessCode = nxf2dzw,
createdAt = 2016 - 09 - 23 T15: 08: 20.673 Z,
id = bda7ec58 - 5 c64 - 4082 - 9 da1 - 48534 dd6afcc,
licenseType = mobileAppLicensesWithDevice,
name = Lead Retrieval - Device Rental & App license,
owner = {
entityType = exhibitor,
id = adc7a8e5 - b137 - 40 c6 - 8765 - deb3ce0b8b3f
},
readableId = b11yew6bqra,
termId = 630493 a4 - 4 a70 - 4 f4f - afaf - 31610 c14c181,
updatedAt = 2016 - 09 - 23 T15: 08: 20.673 Z
}, {
accessCode = 3e7 f1ip,
createdAt = 2016 - 09 - 23 T15: 08: 20.674 Z,
id = 2e12657 a - 8 a62 - 4 d2c - b5d2 - 2 a3fe4c27f9e,
licenseType = mobileAppLicensesWithDevice,
name = Lead Retrieval - Device Rental & App license,
owner = {
entityType = exhibitor,
id = adc7a8e5 - b137 - 40 c6 - 8765 - deb3ce0b8b3f
},
readableId = ye410zlhqe6,
termId = 630493 a4 - 4 a70 - 4 f4f - afaf - 31610 c14c181,
updatedAt = 2016 - 09 - 23 T15: 08: 20.674 Z
}], total = 3
}
*编辑*
实现此目的的代码示例......
def getLicenseId(String eventId, String exhibitorId) {
String licenseId
def apiGetLicenseId = webClient.get(
path: "event_api/events/${eventId}/exhibitors/${exhibitorId}/licenses",
headers: ['Authorization': "api_key $apiKey"],
requestContentType: JSON
)
assert apiGetLicenseId.status == 200 : "API get license ID failed!\nAPI Response: ${apiGetLicenseId.data}"
String index = apiGetLicenseId.data
def slurper = new JsonSlurper().parseText(index)
slurper.each {
println(it)
}
licenseId = apiGetLicenseId.data.results[1].id
println("Index = " + index)
println("Index Length = " + slurper)
println("License Id = " + licenseId)
return licenseId
}
有关此问题是什么以及如何解决的任何想法?