如何在JSON数组中索引两个值?

发布于 2025-01-30 08:01:34 字数 2287 浏览 2 评论 0原文

我正在尝试找到两个匹配值的数组索引。我下面有我的刻板脚本,这给了我时且有效的索引。返回正确的索引。我很难弄清楚的部分是将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-02-06 08:01:34

如果您知道weighteStimatesInq始终将是项目列表的关键,则可以执行类似的事情:

json["WeightEstimatesInq"].findIndexOf {
   it["WhenWeighed"] == "BH" && it["OpSeq"] == "30"
}

它会产生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:

json["WeightEstimatesInq"].findIndexOf {
   it["WhenWeighed"] == "BH" && it["OpSeq"] == "30"
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文