Web services 如何检查键值嵌套列表中的值?

Web services 如何检查键值嵌套列表中的值?,web-services,groovy,jsonslurper,Web Services,Groovy,Jsonslurper,我已经创建了一个RESTWeb服务 在响应中,我有一个嵌套列表,每个列表中有5个键值关联。 我只想检查每个值的格式是否正确(布尔值、字符串或整数) 这就是嵌套列表 {"marches": [ { "id": 13, "libelle": "CAS", "libelleSite": "USA", "siteId": 1, "right": false, "active": true }, {

我已经创建了一个RESTWeb服务

在响应中,我有一个嵌套列表,每个列表中有5个键值关联。 我只想检查每个值的格式是否正确(布尔值、字符串或整数)

这就是嵌套列表

{"marches": [
      {
      "id": 13,
      "libelle": "CAS",
      "libelleSite": "USA",
      "siteId": 1,
      "right": false,
      "active": true
   },
      {
      "id": 21,
      "libelle": "MQS",
      "libelleSite": "Spain",
      "siteId": 1,
      "right": false,
      "active": true
   },
      {
      "id": 1,
      "libelle": "ASCV",
      "libelleSite": "Italy",
      "siteId": 1,
      "right": false,
      "active": true
   }]
}
我使用JsonSlurper类来读取groovy响应

import groovy.json.JsonSlurper
def responseMessage = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(responseMessage)
通过下面的循环,我可以获得列表的每个块

marches.each { n ->
    log.info "Nested $n \n"
}

例如,我想检查与键“id”、“13”相关联的值是否是一个整数等等。

您就快到了。在
内部。每个
代表嵌套对象:

json.marches.each { 
  assert it.id instanceof Integer  // one way to do it

  //another way
  if( !(it.libelle instanceof String) ){
    log.info "${it.id} has bad libelle"
  } 

  //one more way
  return (it.libelleSite instanceof String) &&
     (it.siteId instanceof Integer) && (it.right instanceof Boolean)
}
如果你不关心细节,只想确保它们都是好的,你也可以使用
。每个

assert json.marches.every {
    it.id instanceof Integer &&
    it.libelle instanceof String &&
    it.libelleSite instanceof String &&
    it.active instanceof Boolean  //...and so on
}

你快到了。在
内部。每个
代表嵌套对象:

json.marches.each { 
  assert it.id instanceof Integer  // one way to do it

  //another way
  if( !(it.libelle instanceof String) ){
    log.info "${it.id} has bad libelle"
  } 

  //one more way
  return (it.libelleSite instanceof String) &&
     (it.siteId instanceof Integer) && (it.right instanceof Boolean)
}
如果你不关心细节,只想确保它们都是好的,你也可以使用
。每个

assert json.marches.every {
    it.id instanceof Integer &&
    it.libelle instanceof String &&
    it.libelleSite instanceof String &&
    it.active instanceof Boolean  //...and so on
}

只需使用嵌套的每个…您想对响应执行什么操作?筛选值或获得正确/错误答案?我想检查响应是否属于正确类型。只需使用嵌套的每个值…您想对响应执行什么操作?过滤值或获得正确/错误答案?我想检查响应是否属于正确类型。