Soapui如何从json响应中提取某个值

时间:2016-06-23 11:50:07

标签: json groovy soapui

我正在尝试提取dsId的值,但无法做到这一点。

请查看以下详细信息。

我正在工作的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回复如下。我也想提取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中,您可以看到有一些列名称包含值,如何提取列特定数据?

1 个答案:

答案 0 :(得分: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