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

时间:2016-11-16 01:04:57

标签: json groovy jsonslurper

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

下面是我尝试使用的代码,但是,每次运行脚本时,SOAPUI都会返回错误:“java.lang.NullPointerException:无法在null对象上获取属性'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'

3 个答案:

答案 0 :(得分:0)

假设您的JSON响应良好(通过致电print查看),请尝试将.text添加到jsonSlurper()来电

的末尾

您似乎在parseText(response)之间有空格 所以它应该是

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

但是我会尝试转换为ArrayList<LazyMap>,以确保您可以通过执行迭代 ArrayList<LazyMap> json = new JsonSlurper().parseText(response) as ArrayList<LazyMap>

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

答案 1 :(得分:0)

这行代码不能处理索引解析:

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

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

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

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

答案 2 :(得分:0)

好的,所以我设法通过使用JsonPath而不是JsonSlurper来实现这一点。

为实现这一目标,我必须导入以下内容:

导入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)
}