如何在JSON数组中索引两个值?
我正在尝试找到两个匹配值的数组索引。我下面有我的刻板脚本,这给了我时且有效的索引。返回正确的索引。我很难弄清楚的部分是将OPSEQ添加到索引标准中。
我要做的是在'theighed and opseq时找到索引。例如,我想找到wheyed = bh和opseq = 30的索引。在我的下面的json中应该是4。
有人可以帮助您解释如何在grovy中做到这一点吗?
JSON使用了:
{
"BusinessUnit": "1111111",
"WorkOrder": 1111111,
"WeightEstimatesInq": [
{
"WhenWeighed": "BH",
"WhenWeighedDesc": "Before Heading Weight",
"TotalWeight": 900,
"Weight": 12,
"OpSeq": "10",
"AdditionalNotes": " ",
"TareWeight": " ",
"Effective Date": "null"
},
{
"WhenWeighed": "AH",
"WhenWeighedDesc": "After Heading Weight",
"TotalWeight": 987,
"Weight": 900,
"OpSeq": "10",
"AdditionalNotes": "Weighed Bin 10 5/17/2022",
"TareWeight": "87",
"Effective Date": "null"
},
{
"WhenWeighed": "BO",
"WhenWeighedDesc": "Before OSP Weight",
"TotalWeight": 900,
"Weight": 9,
"OpSeq": "50",
"AdditionalNotes": " ",
"TareWeight": " ",
"Effective Date": "null"
},
{
"WhenWeighed": "AO",
"WhenWeighedDesc": "After OSP Weight",
"TotalWeight": 1000,
"Weight": 750,
"OpSeq": "50",
"AdditionalNotes": " ",
"TareWeight": "150",
"Effective Date": "null"
},
{
"WhenWeighed": "BH",
"WhenWeighedDesc": "Before Heading Weight",
"TotalWeight": 720,
"Weight": 700,
"OpSeq": "30",
"AdditionalNotes": "Weighed Bin 30 5/17/2022",
"TareWeight": "20",
"Effective Date": "null"
}
],
"status": "SUCCESS",
"startTimestamp": "2022-05-17T12:27:49.302-0400",
"endTimestamp": "2022-05-17T12:27:50.279-0400",
"serverExecutionSeconds": 0.977
}
使用的Groovy使用:
// Read Input Values
String aWhenWeighedUDC = aInputMap.WhenWeighedUDC ?: " "
String aInputJson = aInputMap.InputJson ?: "{}"
// Initialize Output Values
def error = " "
def rowNumber = 0
def lastRowNumber = 1
// Parse JSON
def json = new JsonSlurper().parseText( aInputJson )
// Determine Row Numbers
def rowset = json?.WeightEstimatesInq
if ( rowset ) {
rowNumber = rowset*.WhenWeighed.indexOf( aWhenWeighedUDC ) + 1
lastRowNumber = rowset.size()
}
I am trying to find an array index of two matching values. I have my Groovy script below that is giving me the index of WhenWeighed and that works. Returns the correct Index. The part I am having difficulty figuring out is adding OpSeq to the indexing criteria.
What I'm trying to do is find the index of WhenWeighed and OpSeq. For Example, I want to find the index of WhenWeighed = BH and OpSeq = 30. In my below JSON this should be 4.
Can anyone help explain how you do this in Groovy?
JSON Used:
{
"BusinessUnit": "1111111",
"WorkOrder": 1111111,
"WeightEstimatesInq": [
{
"WhenWeighed": "BH",
"WhenWeighedDesc": "Before Heading Weight",
"TotalWeight": 900,
"Weight": 12,
"OpSeq": "10",
"AdditionalNotes": " ",
"TareWeight": " ",
"Effective Date": "null"
},
{
"WhenWeighed": "AH",
"WhenWeighedDesc": "After Heading Weight",
"TotalWeight": 987,
"Weight": 900,
"OpSeq": "10",
"AdditionalNotes": "Weighed Bin 10 5/17/2022",
"TareWeight": "87",
"Effective Date": "null"
},
{
"WhenWeighed": "BO",
"WhenWeighedDesc": "Before OSP Weight",
"TotalWeight": 900,
"Weight": 9,
"OpSeq": "50",
"AdditionalNotes": " ",
"TareWeight": " ",
"Effective Date": "null"
},
{
"WhenWeighed": "AO",
"WhenWeighedDesc": "After OSP Weight",
"TotalWeight": 1000,
"Weight": 750,
"OpSeq": "50",
"AdditionalNotes": " ",
"TareWeight": "150",
"Effective Date": "null"
},
{
"WhenWeighed": "BH",
"WhenWeighedDesc": "Before Heading Weight",
"TotalWeight": 720,
"Weight": 700,
"OpSeq": "30",
"AdditionalNotes": "Weighed Bin 30 5/17/2022",
"TareWeight": "20",
"Effective Date": "null"
}
],
"status": "SUCCESS",
"startTimestamp": "2022-05-17T12:27:49.302-0400",
"endTimestamp": "2022-05-17T12:27:50.279-0400",
"serverExecutionSeconds": 0.977
}
Groovy Used:
// Read Input Values
String aWhenWeighedUDC = aInputMap.WhenWeighedUDC ?: " "
String aInputJson = aInputMap.InputJson ?: "{}"
// Initialize Output Values
def error = " "
def rowNumber = 0
def lastRowNumber = 1
// Parse JSON
def json = new JsonSlurper().parseText( aInputJson )
// Determine Row Numbers
def rowset = json?.WeightEstimatesInq
if ( rowset ) {
rowNumber = rowset*.WhenWeighed.indexOf( aWhenWeighedUDC ) + 1
lastRowNumber = rowset.size()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道
weighteStimatesInq
始终将是项目列表的关键,则可以执行类似的事情:它会产生
4
。您可以通过&&
IT添加更多条件。请注意,如果没有匹配您的条件,则可能会返回
-1
。If you know that
WeightEstimatesInq
is always going to be the key for the list of items, you can do something like this:which will yield
4
. You can add more criteria by&&
it.Note that this has the potential to return
-1
if nothing matches your criteria.