2016-06-23 89 views
1

我試圖提取dsId的值,但無法做到這一點。Soapui如何提取某個值表格json響應

請在下面找到詳細信息。其中我的工作

Groovy腳本是:

import groovy.json.JsonSlurper 
def response = messageExchange.response.responseContent 
log.info response 

def slurper = new JsonSlurper() 
def json = slurper.parseText response 
log.info json 
log.info json.New England Schools__NE Schools$.dsId 

但得到以下錯誤 enter image description here

我的JSON響應如下,請幫助also want to extract value of ZIP

{ 
    "New England Schools__NE Schools$": { 
    "dsId": "ds_608", 
    "type": "Account", 
    "numericCols": [ 
     "ZIP", 
     "Count Free Lunch", 
     "Count Reduced Lunch", 
     "Total Lunch Pgm", 
     "Total Students", 
     "PreKindergarten", 
     "Kindergarten", 
     "Grade 1", 
     "Grade 2", 
     "Grade 3", 
     "Grade 4", 
     "Grade 5", 
     "Grade 6", 
     "Grade 7", 
     "Grade 8", 
     "Grade 9", 
     "Grade 10", 
     "Grade 11", 
     "Grade 12" 
    ], 
    "data": null, 
    "AggNumericData": [ 
     { 
     "ZIP": 19949795, 
     "Count Free Lunch": 263596, 
     "Count Reduced Lunch": 71799, 
     "Total Lunch Pgm": 331987, 
     "Total Students": 1796594, 
     "PreKindergarten": 36553, 
     "Kindergarten": 151898, 
     "Grade 1": 164370, 
     "Grade 2": 169418, 
     "Grade 3": 173748, 
     "Grade 4": 175505, 
     "Grade 5": 179357, 
     "Grade 6": 177978, 
     "Grade 7": 174655, 
     "Grade 8": 170192, 
     "Grade 9": 178423, 
     "Grade 10": 162045, 
     "Grade 11": 147564, 
     "Grade 12": 133368 
     } 
    ], 
    "MinMax": [], 
    "dsData": "Account Id#%#Territory#%#District#%#Area#%#Region#%#objname#%#~ID~#%#~Lat-Lon Linked~#%#~Latitude~#%#~Longitude~#%#~Lat-Lon Zip~#%#School Name#%#Address#%#City#%#State#%#ZIP#%#ZIP4#%#School Type#%#Status#%#School Level#%#Count Free Lunch#%#Count Reduced Lunch#%#Total Lunch Pgm#%#Total Students#%#PreKindergarten#%#Kindergarten#%#Grade 1#%#Grade 2#%#Grade 3#%#Grade 4#%#Grade 5#%#Grade 6#%#Grade 7#%#Grade 8#%#Grade 9#%#Grade 10#%#Grade 11#%#Grade 12#%#Territory1#%#Region1#%#lat#%#lon#%#terrid\r\n15709#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15709#%#True#%#41.934711#%#-72.770021#%#06026#%#R. DUDLEY SEYMOUR SCHOOL#%#185 HARTFORD AVENUE#%#EAST GRANBY#%#CT#%#6026#%#9520#%#1#%#1#%#2#%#0#%#0#%#0#%#131#%#0#%#0#%#0#%#0#%#0#%#60#%#71#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5151204.33051376#%#-8100721.57141633#%#3\r\n15707#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15707#%#True#%#41.934894#%#-72.730656#%#06026#%#EAST GRANBY HIGH SCHOOL#%#95 SOUTH MAIN STREET#%#EAST GRANBY#%#CT#%#6026#%#9550#%#1#%#1#%#3#%#0#%#0#%#0#%#219#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#57#%#55#%#53#%#54#%#Hartford, CT#%#New England#%#5151231.26605957#%#-8096340.03625871#%#3\r\n15708#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15708#%#True#%#41.934894#%#-72.730656#%#06026#%#EAST GRANBY MIDDLE SCHOOL#%#95 SOUTH MAIN STREET#%#EAST GRANBY#%#CT#%#6026#%#9550#%#1#%#1#%#2#%#0#%#0#%#0#%#201#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#67#%#73#%#61#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5151231.26605957#%#-8096340.03625871#%#3\r\n15706#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15706#%#True#%#41.944215#%#-72.732696#%#06026#%#ALLGROVE SCHOOL#%#33 TURKEY HILLS ROAD#%#EAST GRANBY#%#CT#%#6026#%#9570#%#1#%#1#%#1#%#0#%#0#%#0#%#275#%#3#%#69#%#65#%#82#%#56#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5152627.52929053#%#-8096567.12801993#%#3\r\n15710#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15710#%#True#%#41.944215#%#-72.732696#%#06026#%#HOMEBOUND#%#33 TURKEY HILL ROAD#%#EAST GRANBY#%#CT#%#6026#%#674#%#4#%#3#%#4#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5152627.52929053#%#-8096567.12801993#%#3\r\n15923#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15923#%#True#%#42.0027#%#-72.942#%#06027#%#HOMEBOUND#%#30 SOUTH ROAD#%#EAST HARTLAND#%#CT#%#6027#%#9710#%#4#%#3#%#4#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5161383.89953631#%#-8119866.29744296#%#3\r\n15922#%#Hartford, CT#%#New England#%#Unassigned#%#Unassigned#%#Account#%#15922#%#True#%#42.0027#%#-72.942#%#06027#%#HARTLAND ELEMENTARY SCHOOL#%#30 SOUTH ROAD#%#EAST HARTLAND#%#CT#%#6027#%#9710#%#1#%#1#%#1#%#0#%#0#%#0#%#2#%#0#%#25#%#17#%#26#%#29#%#37#%#36#%#38#%#35#%#40#%#0#%#0#%#0#%#0#%#Hartford, CT#%#New England#%#5161383.89953631#%#-8119866.29744296#%#3\r\n16335#%#} 

喜在dsData你可以看到有一些帶有值的列名如何提取特定數據?

+0

喜饒@ PLZ看看此http://計算器。com/questions/38007809/soapui-how-to-extract-column-specific-data-form-the-json-response –

回答

1

你得到了非常微不足道的錯誤。

幾件事情:

  • 該物業已得到空格字符:所以它需要報價
  • 內封閉有$也需要被轉義。

這裏是一個可以檢索的dsId

所需的值從改變你的最後一條語句聲明:

log.info json.New England Schools__NE Schools$.dsId

要:

log.info json."New England Schools__NE Schools\$".dsId

希望這會有所幫助。

更新

只注意到你想提取ZIP值,它恰好是第一要素的屬性陣列AggNumericData英寸此前,您的帖子中的這個問題不可見,或者我忽略了。所以在我的答案更新相同。

這裏得到同樣的聲明:

log.info ​json."New England Schools__NE Schools\$".AggNumericData[0].ZIP​

更新2:提到的問題作者,他是不是不能夠獲取數據一起在他的評論。所以,在這裏添加如何獲得這兩個。請注意,由於只提供了部分json,我不得不使用最少的數據來重現它。

獲取包括請求數據

enter image description here

+0

嗨,感謝Buddy首先非常感謝您的快速回復,我可以找到dsId與您的解決方案,但是何時試圖讓zip獲取下面的錯誤,像這樣的啓動失敗:Script17.groovy:9:無效的變量名稱。必須以字母開頭,但是:json。在[9:10] @第9行,第10列。log.info json。「New England Schools__NE Schools \ $」。AggNumericData [0] .ZIP^org.codehaus.groovy.syntax.SyntaxException:變量名稱無效。必須以字母開頭,但是:json。在[9:10] @第9行,第10列。org.codehaus.groovy.control.CompilationUnit $ 15.call(CompilationUnit.java:760) –

+0

根據你的示例,我得到了給定語句的zip。但是,您的實際要求可能與您提供的要求有所不同並造成這種情況?你想提供更多信息? – Rao

+0

你好奇怪的,當我只使用郵政編碼查找代碼其工作,但是當我同時使用兩個同時它不工作 –