0

我正在尝试使用存储在名为“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'
4

3 回答 3

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')

于 2016-11-16T05:28:12.500 回答
0

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

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

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

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

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

于 2016-11-16T14:36:58.877 回答
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)
}
于 2016-11-17T00:43:40.327 回答