httpBuilder JSON 响应解析包括 [ ]

httpBuilder JSON response parse includes [ ]

这可能很明显,所以请保持温和。

以下代码:

def http = new HTTPBuilder(uri)

http.request(POST,JSON ) { req ->
    headers.'Content-Type' = 'application/json'
    headers.'x-chkp-sid' = CHKPsid
    body = [
            "limit" : 100,
            "offset" : 0,
            "details-level" : "standard"
    ]

    response.success = { resp, json ->
       println (json)
       println "POST Success. SHOWGWS: ${resp.statusLine}"
       println json.objects.name
       println json.size()
       println json.getClass()
     assert json.objects.name == 'gw-6eee89'
    }
}

给出以下输出:

[objects:[[uid:892d08fb-0dca-5146-8587-49fa07ece24c, name:gw-6eee89, type:simple-gateway, domain:[uid:41e821a0-3720-11e3-aa6e-0800200c9fde, name:SMC User, domain-type:domain]]], from:1, to:1, total:1]

POST Success. SHOWGWS: HTTP/1.1 200 OK

[gw-6eee89]

4

class groovy.json.internal.LazyMap

Caught: Assertion failed: 

   assert json.objects.name == 'gw-6eee89'
       |    |       |    |
       |    |       |    false
       |    |       [gw-6eee89]
       |    [[uid:892d08fb-0dca-5146-8587-49fa07ece24c, name:gw-6eee89, type:simple-gateway, domain:[uid:41e821a0-3720-11e3-aa6e-0800200c9fde, name:SMC User, domain-type:domain]]]
       [objects:[[uid:892d08fb-0dca-5146-8587-49fa07ece24c, name:gw-6eee89, type:simple-gateway, domain:[uid:41e821a0-3720-11e3-aa6e-0800200c9fde, name:SMC User, domain-type:domain]]], from:1, to:1, total:1]

似乎是在比较 gw-6eee89 和 [gw-6eee89]

我不明白为什么在调用值时没有删除方括号。非常感谢任何帮助。

因为 objectsList 的一个实例,如果您调用 list.someProperty 您将获得列表中所有对象的 someProperty 值列表:

[[name:1],[name:2]].name == [1, 2]

您需要获取第一个对象。例如:objects.name[0].