soapUI - 包含数组的Parse JSON Response

时间:2014-10-29 09:12:17

标签: arrays json parsing groovy soapui

给出soapUI中先前测试步骤(请求)的以下JSON响应:

{
  "AccountClosed": false,
  "AccountId": 86270,
  "AccountNumber": "2915",
  "AccountOwner": 200000000001,
  "AccountSequenceNumber": 4,
  "AccountTotal": 6,
  "ActiveMoneyBeltSession": true,
  "CLMAccountId": "",
  "CustomerName": "FRANK",
  "Lines": [{
    "AndWithPreviousLine": false,
    "IngredientId": 10000025133,
    "OrderDestinationId": 1,
    "PortionTypeId": 1,
    "Quantity": 1,
    "QuantityAsFraction": "1",
    "SentToKitchen": true,
    "TariffPrice": 6,
    "Id": 11258999068470003,
    "OriginalRingUpTime": "2014-10-29T07:37:38",
    "RingUpEmployeeName": "Andy Bean",
    "Seats": []
  }, {
    "Amount": 6,
    "Cashback": 0,
    "Change": 0,
    "Forfeit": 0,
    "InclusiveTax": 1,
    "PaymentMethodId": 1,
    "ReceiptNumber": "40/1795",
    "Tip": 0,
    "Id": 11258999068470009,
    "Seats": []
  }],
  "MoaOrderIdentifier": "A2915I86270",
  "OutstandingBalance": 0,
  "SaveAccount": false,
  "ThemeDataRevision": "40",
  "TrainingMode": false
}

我有以下groovy脚本来解析响应中的信息:

import groovy.json.JsonSlurper

//Define Variables for each element on JSON Response
String AccountID, AccountClosed, AccountOwner, AccountSeqNumber, AccountTotal, ActMoneyBelt
String CLMAccountID, ThemeDataRevision, Lines, MoaOrderIdentifier, OutstandingBalance, SaveAccount, TrainingMode, CustName, AccountNumber
String AndWithPreviousLine, IngredientId, OrderDestinationId, PortionTypeId, Quantity, SentToKitchen, TariffPrice, Id, OriginalRingUpTime, RingUpEmployeeName, Seats
int AccSeqNumInt
def responseContent = testRunner.testCase.getTestStepByName("ReopenAcc").getPropertyValue("response")
  // Create JsonSlurper Object to parse the response
def Response = new JsonSlurper().parseText(responseContent)
  //Parse each element from the JSON Response and store in a variable
AccountClosed = Response.AccountClosed
AccountID = Response.AccountId
AccountNumber = Response.AccountNumber
AccountOwner = Response.AccountOwner
AccountSeqNumber = Response.AccountSequenceNumber
AccountTotal = Response.AccountTotal
ActMoneyBelt = Response.ActiveMoneyBeltSession
CLMAccountID = Response.CLMAccountId
CustName = Response.CustomerName
Lines = Response.Lines
  /*Lines Variables*/
AndWithPreviousLine = Response.Lines.AndWithPreviousLine
IngredientId = Response.Lines.IngredientId
OrderDestinationId = Response.Lines.OrderDestinationId
PortionTypeId = Response.Lines.PortionTypeId
Quantity = Response.Lines.Quantity
SentToKitchen = Response.Lines.SentToKitchen
TariffPrice = Response.Lines.TariffPrice
Id = Response.Lines.Id
OriginalRingUpTime = Response.Lines.OriginalRingUpTime
RingUpEmployeeName = Response.Lines.RingUpEmployeeName
Seats = Response.Lines.Seats
  /*End Lines Variables*/
MoaOrderIdentifier = Response.MoaOrderIdentifier
OutstandingBalance = Response.OutstandingBalance
SaveAccount = Response.SaveAccount
ThemeDataRevision = Response.ThemeDataRevision
TrainingMode = Response.TrainingMode

如上所示,有一个名为“Lines”的元素(Array)。我希望获得此数组中的单个元素值。解析元素时上面的代码返回以下内容:

----------------行变量----------------

INFO: AndWithPreviousLine: [false, null]
INFO: IngredientId: [10000025133, null]
INFO: OrderDestinationId: [1, null]
INFO: PortionTypeId: [1, null]
INFO: Quantity: [1.0, null]
INFO: SentToKitchen: [true, null]
INFO: TariffPrice: [6.0, null]
INFO: Id: [11258999068470003, 11258999068470009]
INFO: OriginalRingUpTime: [2014-10-29T07:37:38, null]
INFO: RingUpEmployeeName: [Andy Bean, null]
INFO: Seats: [[], []]

如何从上面获得所需的元素,还要注意2个不同的数据协定包含相同的元素“ID”。有没有办法区分2。

任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:1)

可以将响应中的“行”视为简单的地图列表。

Response["Lines"].each { println it }

[AndWithPreviousLine:false, Id:11258999068470003, IngredientId:10000025133, OrderDestinationId:1, OriginalRingUpTime:2014-10-29T07:37:38, PortionTypeId:1, Quantity:1, QuantityAsFraction:1, RingUpEmployeeName:Andy Bean, Seats:[], SentToKitchen:true, TariffPrice:6]
[Amount:6, Cashback:0, Change:0, Forfeit:0, Id:11258999068470009, InclusiveTax:1, PaymentMethodId:1, ReceiptNumber:40/1795, Seats:[], Tip:0]

Response只是一个巨大的地图,Lines元素是地图列表。

使用groovysh真的有助于交互式地播放数据,我在启动“groovysh”控制台后执行了以下命令:

import groovy.json.JsonSlurper
t = new File("data.json").text
r = new JsonSlurper().parse(t)
r
r["Lines"]
r["Lines"][0]

编辑:在回复您的评论时,这是获得具有有效AndWithPreviousLine值的条目的更好方法。

myline = r["Lines"].find{it.AndWithPreviousLine != null}
myline.AndWithPreviousLine
>>> false
myline.IngredientId
>>> 10000025133

如果您使用findAll代替find,您将获得一系列地图并且必须为它们编制索引,但是对于这种情况,只有1和find为您提供第一个条件是真的。

通过使用Response["Lines"].AndWithPreviousLine [0],您假设每个条目都存在AndWithPreviousLine,这就是您在列表中获取空值的原因。如果json响应已被反转,则必须使用[1]的不同索引,因此使用findfindAll比假设索引值更适合您。