0

我有一个关于如何使用 groovy 在 JSR223 后处理器中获取 json 数组长度的查询。“outBound”数组中有 2 个信息块。我需要获取数组“outBound”的长度,以便可以将长度放入 for 循环中。我还想获得一个包含参数“taxType”:“GST”的信息 json 数组(原样)。例如:这里第二个信息有 GST 值.. 所以想获取第二个信息 json 数组

{
      "details": [
        {
          "outBound": [
            {
              "info": {
                "date": "2016-08-11",
                "class": "M",
                "code": 70,
                "pricing": [
                  {
                    "totalAmount": 68.8,
                    "totalTaxAmount": 30.8,
                    "baseFareAmount": 38.0
                  }
                ],
                "totalAmount": 68.8,
                "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "id": 1
          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 68.8,
            "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        },
        {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 78.8,
                "totalTaxAmount": 40.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "id": 2,
            "gstAmount": {
          "taxType": "GST"
        },

          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "200"

            },
             { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        }
      ],
      "resultCount": {
        "count1": 1,
        "count2": 1
      },
      "displayCount": 2
    }
  ]
    }

>Expected Output: 

    {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 78.8,
                "totalTaxAmount": 40.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "id": 2,
            "gstAmount": {
          "taxType": "GST"
            },

          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "200"

            },
             { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        }
4

1 回答 1

0

不太确定我是否理解,但如果我这样做了,这就是您所需要的:

def res = new groovy.json.JsonSlurper().parseText(json)
def outBound = res.details[0].outBound
println outBound.size()
println groovy.json.JsonOutput.toJson(outBound.find { it.info.gstAmount?.taxType == "GST" })

json作为一个String持有你的json的实例。

逐行:

  1. Groovy 使用您的 Json。
  2. 您从outBound数组的第一项检索details数组
  3. 打印outBound数组的大小
  4. outBound将属性为“GST”的数组的第一个元素格式化回 Json info.gstAmout.taxType(后面的问号gstAmount很好地处理了该属性可能不存在的情况)
于 2016-09-29T21:41:23.097 回答