如何索引 JSON 数组中的两个值?
How do you index two value in JSON arrays?
我正在尝试查找两个匹配值的数组索引。我在下面有我的 Groovy 脚本,它为我提供了 WhenWeighed 的索引并且可以正常工作。 Returns 正确的索引。我难以理解的部分是将 OpSeq 添加到索引标准中。
我要做的是找到 WhenWeighed 和 OpSeq 的索引。例如,我想找到 WhenWeighed = BH 和 OpSeq = 30 的索引。在我下面 JSON 这应该是 4.
谁能帮忙解释一下 Groovy 你是怎么做到的?
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()
}
如果您知道 WeightEstimatesInq
始终是项目列表的键,您可以这样做:
json["WeightEstimatesInq"].findIndexOf {
it["WhenWeighed"] == "BH" && it["OpSeq"] == "30"
}
这将产生 4
。您可以通过 &&
添加更多条件。
请注意,如果没有符合您的条件,这有可能 return -1
。
我正在尝试查找两个匹配值的数组索引。我在下面有我的 Groovy 脚本,它为我提供了 WhenWeighed 的索引并且可以正常工作。 Returns 正确的索引。我难以理解的部分是将 OpSeq 添加到索引标准中。
我要做的是找到 WhenWeighed 和 OpSeq 的索引。例如,我想找到 WhenWeighed = BH 和 OpSeq = 30 的索引。在我下面 JSON 这应该是 4.
谁能帮忙解释一下 Groovy 你是怎么做到的?
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()
}
如果您知道 WeightEstimatesInq
始终是项目列表的键,您可以这样做:
json["WeightEstimatesInq"].findIndexOf {
it["WhenWeighed"] == "BH" && it["OpSeq"] == "30"
}
这将产生 4
。您可以通过 &&
添加更多条件。
请注意,如果没有符合您的条件,这有可能 return -1
。