Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays soapUI-解析包含数组的JSON响应_Arrays_Json_Parsing_Groovy_Soapui - Fatal编程技术网

Arrays soapUI-解析包含数组的JSON响应

Arrays soapUI-解析包含数组的JSON响应,arrays,json,parsing,groovy,soapui,Arrays,Json,Parsing,Groovy,Soapui,给定soapUI中先前测试步骤(请求)的以下JSON响应: { “账户关闭”:假, “帐户ID”:86270, “帐号”:“2915”, “账户所有者”:2000000000001, “AccountSequenceNumber”:4, “会计总计”:6, “ActiveMoneyBeltSession”:正确, “CLMAccountId”:“, “客户名称”:“弗兰克”, “行”:[{ “AndWithPreviousLine”:false, “IngredientId”:10000025

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

{
“账户关闭”:假,
“帐户ID”:86270,
“帐号”:“2915”,
“账户所有者”:2000000000001,
“AccountSequenceNumber”:4,
“会计总计”:6,
“ActiveMoneyBeltSession”:正确,
“CLMAccountId”:“,
“客户名称”:“弗兰克”,
“行”:[{
“AndWithPreviousLine”:false,
“IngredientId”:10000025133,
“OrderDestinationId”:1,
“PortionTypeId”:1,
“数量”:1,
“QuantityAsFraction”:“1”,
“SentToKitchen”:没错,
“关税价格”:6,
“Id”:11258999068470003,
“原始运行时间”:“2014-10-29T07:37:38”,
“RingUpEmployeeName”:“Andy Bean”,
“席位”:[]
}, {
“金额”:6,
“现金返还”:0,
“更改”:0,
“没收”:0,
“包容性ETAX”:1,
“PaymentMethodId”:1,
“收据编号”:“40/1795”,
“提示”:0,
“Id”:11258999068470009,
“席位”:[]
}],
“订单标识符”:“A2915I86270”,
“未结清余额”:0,
“储蓄账户”:假,
“主题数据修订版”:“40”,
“培训模式”:错误
}
响应中的“行”可以被视为一个简单的地图列表

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]
响应只是一张巨大的地图,其中Lines元素是一个地图列表

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]
在这里使用groovysh确实有助于以交互方式处理数据,在启动“groovysh”控制台后,我执行了以下命令:

编辑:为了回应您的评论,这里有一个更好的方法来获取具有有效ANDWITHPREVIEWLINE值的条目

myline = r["Lines"].find{it.AndWithPreviousLine != null}
myline.AndWithPreviousLine
>>> false
myline.IngredientId
>>> 10000025133
如果您使用
findAll
而不是
find
,您将获得一个映射数组,并且必须对它们进行索引,但是在这种情况下,只有1个,而
find
将为您提供第一个条件为真的映射


通过使用
Response[“Lines”].AndWithPreviousLine[0]
可以假设每个条目上都存在AndWithPreviousLine,这就是为什么列表中会出现空值。如果json响应被反转,您必须使用不同的索引[1],因此使用
find
findAll
将比假设索引值更适合您。

请编辑此问题,它目前不可读。请告诉我您需要澄清的信息,我不知道如何让它更清晰。也许没有必要让它更清晰,但格式很好——也许我没有做到精确。目前代码不可读,例如json。当然可以,谢谢。感谢您的回复,上面的内容对我很有用,可以从列表中提取相关元素,例如log.info(回复[“行]。和WithPreviousLine[0])@blader很高兴我可以help@blader使用
响应[“行”]和上一行[0]执行的操作
假设您的所有行都有这个元素,首先,您可以看到,唯一的公共元素是Id字段。如果您想要具有非null且WithPreviousLine值的条目,最好使用
响应[“Lines”]。找到{it.AndWithPreviousLine!=null}
,这将为包含该数据的条目提供整个映射条目,将
和WithPreviousLine
添加到该条目的末尾以获取其值。我会更新我的答案,让这更清楚。