如何使用 JSONSlurper 在 groovy 中对 JSON 数组响应执行断言



我正在尝试使用存储在名为"jsonFieldName"的变量中的值的位置,从 JSON 数组响应中提取名称值"Acura"。

下面是我尝试执行此操作的代码,但是,每次运行脚本时,SOAPUI 都会返回错误:"java.lang.NullPointerException:无法在空对象错误上获取属性'name',错误在行:156">

有人可以建议如何做到这一点吗?

import groovy.json.JsonSlurper
def response = '''{
"makes": [
{
  "id": 200002038,
  "name": "Acura",
  "niceName": "acura",
  "models": [
    {
      "id": "Acura_ILX",
      "name": "ILX",
      "niceName": "ilx",
      "years": [
        {
          "id": 200471908,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_ILX_Hybrid",
      "name": "ILX Hybrid",
      "niceName": "ilx-hybrid",
      "years": [
        {
          "id": 200493809,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_MDX",
      "name": "MDX",
      "niceName": "mdx",
      "years": [
        {
          "id": 200465929,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_RDX",
      "name": "RDX",
      "niceName": "rdx",
      "years": [
        {
          "id": 200467168,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_RLX",
      "name": "RLX",
      "niceName": "rlx",
      "years": [
        {
          "id": 100539511,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_TL",
      "name": "TL",
      "niceName": "tl",
      "years": [
        {
          "id": 200488448,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_TSX",
      "name": "TSX",
      "niceName": "tsx",
      "years": [
        {
          "id": 200490517,
          "year": 2014
        }
      ]
    },
    {
      "id": "Acura_TSX_Sport_Wagon",
      "name": "TSX Sport Wagon",
      "niceName": "tsx-sport-wagon",
      "years": [
        {
          "id": 200673755,
          "year": 2014
        }
      ]
    }
  ]
},
{
  "id": 200001769,
  "name": "Aston Martin",
  "niceName": "aston-martin",
  "models": [
    {
      "id": "Aston_Martin_DB9",
      "name": "DB9",
      "niceName": "db9",
      "years": [
        {
          "id": 200473436,
          "year": 2014
        }
      ]
    },
    {
      "id": "Aston_Martin_Rapide_S",
      "name": "Rapide S",
      "niceName": "rapide-s",
      "years": [
        {
          "id": 200460643,
          "year": 2014
        }
      ]
    },
    {
      "id": "Aston_Martin_V8_Vantage",
      "name": "V8 Vantage",
      "niceName": "v8-vantage",
      "years": [
        {
          "id": 200472947,
          "year": 2014
        }
      ]
    },
    {
      "id": "Aston_Martin_Vanquish",
      "name": "Vanquish",
      "niceName": "vanquish",
      "years": [
        {
          "id": 200431313,
          "year": 2014
        }
      ]
    }
  ]
}
],
"makesCount": 2
}'''
def jsonFieldName = ('makes[0].name')
def json = new JsonSlurper().parseText (response)
jsonFieldName.split("\.").each{json = json[it]}
assert json == 'Acura'

假设您的 JSON 从响应中是好的(通过调用print进行检查( 尝试在jsonSlurper()通话结束时添加.text

看起来你在parseText(response)之间有一个空间所以应该是

def json = new JsonSlurper().parseText(response)

但是,我会尝试投射到ArrayList<LazyMap>以确保您可以迭代
ArrayList<LazyMap> json = new JsonSlurper().parseText(response) as ArrayList<LazyMap>

然后致电:
json.get('Acura')

此行代码不处理索引解析:

jsonFieldName.split("\.").each{json = json[it]}

没有名称为 makes[0] 的密钥。相反,有一个makes数组,您对第一个感兴趣。以下硬编码行检索 name 属性:

def result = json.'makes'[0].'name'

正如您在此处看到的,还有一个额外的步骤来解析索引运算符。当然,您可以自己实现此功能,也可以使用 JsonPath 代替 JsonSlurper

好的,

所以我设法通过使用JsonPath而不是JsonSlurper来工作。

为此,我必须导入以下内容:

import com.jayway.jsonpath.jsonPath

def jsonFieldName = "makes[0].name"
def expectedValue = "Acura"
def jsonSuff = JsonPath.read(response, jsonFieldName)
log.info(jsonSuff)
if (jsonSuff.toString() == expectedValue.toString()){
    log.info("Actual value"+jsonSuff+"is equal to expected value"+expectedValue)
}

最新更新