我不知道,我真的很瞭解情況,似乎正在接收具有JSON中的一個節點屬性的XML 響應 ...是不是?
儘管這樣我的理解基本上是要檢查您JSON所有endpoints
條目包含了所有必需的屬性:label
,branch
,appname
;並且在每個端點中的所有branches
都包含url
,name
,api_version
和label
。
所以一種可能的方法是使用JsonSlurper
並檢查元素是否不是null
。喜歡的東西:
import groovy.json.JsonSlurper
def jsonTxt = '''{
"timestamp": "2016-04-14T17:53:29Z",
"id": "544cc493-8f4a-4f14-b95b-2c127f54caac",
"endpoints": [
{
"label": "Gify",
"branches": [
{
"url": "/gify/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify"
},
{
"label": "x1",
"branches": [
{
"url": "/x1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify2"
}
]
}'''
// parse json
def json = new JsonSlurper().parseText(jsonTxt)
// for each endpoint
json.endpoints.each { endpoint ->
// check that label is not null
assert endpoint.label != null, 'ENDPOINT ENTRY NOT CONTAINS LABEL'
// check that appname is not null
assert endpoint.appname != null, 'ENDPOINT ENTRY NOT CONTAINS APPNAME'
// ...
assert endpoint.branches != null, 'ENDPOINT ENTRY NOT CONTAINS BRACHES'
// for each branch
assert endpoint.branches.each { branch ->
// and so on...
assert branch.url != null, 'BRANCH ENTRY NOT CONTAINS URL'
assert branch.name != null, 'BRANCH ENTRY NOT CONTAINS NAME'
assert branch.api_version != null, 'BRANCH ENTRY NOT CONTAINS API_VERSION'
assert branch.label != null, 'BRANCH ENTRY NOT CONTAINS LABEL'
}
}
UPDATE
不大可能XML和XSD,沒有架構來驗證您的的Json,但是你可以創建一個模板來驗證你的迴應反對使用JsonSlurper
。由於您只想來檢查name
S中的元素不是它的值,你可以創建一個函數來遞歸比較name
S:
import groovy.json.JsonSlurper
// compare json against the "schema"
def compareJsonNames(json,schema) {
if(json instanceof Map){
// it's a map... check all names
assert(json.keySet() == schema.keySet())
// for every element in a map...
json.eachWithIndex { it,index ->
def key = schema.keySet().getAt(index)
return compareJsonNames(it.value, schema.find{ e -> e.key == key}.value)
}
}else if(json instanceof List){
// it's a list, compare its elements
json.eachWithIndex { it, index ->
return compareJsonNames(it,schema[index])
}
}
// it's a simple value nothing to do
}
def jsonTxt = '''{
"timestamp": "2016-04-14T17:53:29Z",
"id": "544cc493-8f4a-4f14-b95b-2c127f54caac",
"endpoints": [
{
"label": "Gify",
"branches": [
{
"url": "/gify/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify"
},
{
"label": "x1",
"branches": [
{
"url": "/x1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify2"
}
]
}'''
// template to validate the names
def template = '''{
"label": "",
"branches": [
{
"url": "",
"name": "",
"api_version": "",
"label": ""
}
],
"appname": ""
}'''
def slurper = new JsonSlurper()
// parse your response and the expected template
def json = slurper.parseText(jsonTxt)
def jsonTemplate = slurper.parseText(template)
// check if each endpoint are well formed against the template
json.endpoints.each {
compareJsonNames(it,jsonTemplate)
}
希望它能幫助,
你有什麼這麼遠嗎?你有什麼嘗試?你應該能夠收集不同的值,並檢查你有相同數量的密鑰 –
我試過但def endpoint = context.expand('$ {login#Endpoint}') def response = context.expand('$ {login#Response#declare namespace ns1 = \'https://au.io/ns/201 \'; // ns1:login_resp [1]/ns1:item [1]/ns1:response [1]}') def label1 endpoint.label [0] =(「label」) assert endpoint.contains(label1) –
你可以把它放在問題中嗎?代碼是不可能的在評論 –