2016-11-16 91 views
0

我在嘗試使用存儲在名爲「jsonFieldName」變量中的值的位置從JSON數組響應中提取名稱值「Acura」。如何使用JSONSlurper在常規JSON數組響應中執行斷言

下面是我試圖做到這一點的代碼,但是,每次我運行腳本,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' 

回答

0

假設您的JSON是從響應好(通過調用print檢查)嘗試添加.textjsonSlurper()通話結束

它也像你parseText(response) 所以之間的空間它應該是

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

不過我會嘗試鑄造ArrayList<LazyMap>確保您可以通過
ArrayList<LazyMap> json = new JsonSlurper().parseText(response) as ArrayList<LazyMap>

迭代,然後調用:
json.get('Acura')

0

這行代碼不處理索引決議:

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

沒有鑰匙與名稱makes[0]。相反,有一組makes,你對第一個感興趣。下面的硬編碼線檢索name屬性:

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

正如你可以在這裏看到有解決的索引操作的額外步驟。當然你可以自己實現這個功能,或者使用JsonPath而不是JsonSlurper

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