【问题标题】:How to use JSONSlurper to perform assertion on JSON array response in groovy如何使用 JSONSlurper 在 groovy 中对 JSON 数组响应执行断言
【发布时间】:2016-11-16 01:04:56
【问题描述】:

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

下面是我尝试执行此操作的代码,但是,每次我运行脚本时,SOAPUI 都会返回错误:“java.lang.NullPointerException: Cannot get property 'name' on null object error at line: 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 groovy jsonslurper


    【解决方案1】:

    假设您的 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')

    【讨论】:

      【解决方案2】:

      这行代码不处理索引分辨率:

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

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

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多