0

groovy を使用して JSR223 ポスト プロセッサで json 配列の長さを取得する方法に関連するクエリがあります。「outBound」配列内に 2 つの情報ブロックがあります。長さを for ループに入れることができるように、配列「outBound」の長さを取得する必要があります。また、パラメーター "taxType": "GST" を含む情報 json 配列 (そのまま) を取得したいと考えています。例: ここで 2 番目の情報には GST 値が含まれているため、2 番目の情報 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" })

jsonStringあなたのjsonを保持するインスタンスです。

1行ずつ:

  1. Groovy は Json を消費します。
  2. outBound配列の最初の項目から配列をdetails取得します
  3. outBound配列のサイズの出力
  4. outBoundプロパティが「GST」である配列の最初の要素をJsonにフォーマットし直しinfo.gstAmout.taxTypeます(後の疑問符gstAmountは、そのプロパティが存在しない可能性を適切に処理します)
于 2016-09-29T21:41:23.097 に答える