繁体   English   中英

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

[英]How to use JSONSlurper to perform assertion on JSON array response in groovy

我试图通过使用存储在名为“ jsonFieldName”的变量中的值的位置来编写从JSON数组响应中提取名称值“ Acura”的操作。

下面是我尝试使用的代码,但是,每次我运行脚本时,SOAPUI都会返回错误:“ java.lang.NullPointerException:在第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检查),尝试将.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')

您的代码的这一行不处理索引解析:

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

没有名称为makes[0] 取而代之的是一个数组makes和你有兴趣放在了第一位。 以下硬编码行检索name属性:

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

如您所见,这里还有一个解决索引运算符的附加步骤。 当然,您可以自己实现此功能,也可以使用JsonPath代替JsonSlurper

好的,所以我设法通过使用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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM