从数组中找到的值创建新数组(JSON + Groovy)

时间:2014-04-24 11:35:03

标签: json groovy

我有一个数组:

R = [ [ Period:[ Price:[x:1, Date:2011, NewPrice:1.000],
                 PriceX:[PriceY:2, Date:2012, NewPrice:1.000] ] ] ]
例如,

。我需要使用元素Date的值创建新数组:

 dates = [2011, 2012]

(日期可以存在于此数组的任何位置)。我可以通过循环中的路径创建:

import groovy.json.JsonSlurper

def R = newJsonSlurper().parseText(String.valueOf(messageExchange.getResponseContent()));

M = R.Period
size = M.size()
def dates = []

for ( i in 0.. (size-1))
{
    def y = M[i].Date
    dates.add(y)
}

但也许我可以通过方法findAll()或其他方法创建数组?

响应:

{
  "F":{
    "Stan":{
      "Form":{
        "Fin":{
          "Period":[
            {
              "PeriodEnd":"2013-12-31",
              "PeriodF":{
                "PeriodF":[
                  {
                    "PeriodEndDate":"2013-10-10",
                    "PeriodFiling":{
                      "C":{
                        "X":"B",
                        "RepTo":"1.0",
                        "Reported":"B"
                      },
                      "Final":"1",
                      "Or":"2014-01:47:00",
                      "Units":{
                        "X":"M",
                        "Reported":"M"
                      }
                    },
                    "Period":"A",
                    "SDate":"2013-12-31",
                    "Stat":{
                      "Stat":[
                        {
                          "Fi":{
                            "FV":[
                              {
                                "CO":"R",
                                "VALUE":"1"
                              }
                            ]
                          },
                          "PeriodEnd":"2013-12-31",
                          "Period":"An",
                          "De":"2013-12-31",
                          "S":{
                            "Complete":"1",
                            "C":{
                              "X":"B",
                              "CRte":"1",
                              "X":"B"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            {
              "PeriodEnd":"2011-12-31",
              "Period":{
                "Period":[
                  {
                    "PeriodEndDate":"2012-10-10",
                    "Period":{
                      "Cur":{
                        "X":"BL",
                        "Rep":"1.00",
                        "R":"BL"
                      },
                      "Final":"1",
                      "Original":"2014-01:47",
                      "Units":{
                        "X":"M",
                        "R":"M"
                      }
                    },
                    "Period":"A",
                    "StatDate":"2013-12-31",
                    "Stat":{
                      "Stat":[
                        {
                          "Fin":{
                            "FV":[
                              {
                                "C":"R",
                                "V":"1.000000"
                              }
                            ]
                          },
                          "PeriodEnd":"2013-12-31",
                          "Period":"A",
                          "D":"2013-12-31",
                          "Statement":{
                            "Complete":"1",
                            "Currencies":{
                              "X":"B",
                              "C":"1",
                              "X":"B"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

假设:

def arr = [ [ Period:[ Price :[x:1, Date:2011, NewPrice:1.000],
                       PriceX:[PriceY:2, Date:2012, NewPrice:1.000] ] ] ]

你可以这样做:

def dates = arr.Period.collectMany { it.collect { k, v -> v.Date } }
assert dates == [ 2011, 2012 ]

或者

def dates = arr.Period*.values().Date.flatten()

发布JSON后编辑:

因此,假设你想要PeriodEnd,你可以这样做:

new JsonSlurper().parseText( response )
                 .F
                 .Stan
                 .Formation
                 .Fin
                 .Period
                 .PeriodEnd

或者,如果您想要StatDate的所有值,那么您可以这样做:

new JsonSlurper().parseText( response )
                 .F
                 .Stan
                 .Formation
                 .Fin
                 .Period
                 .PeriodFilings
                 .PeriodFiling
                 .StatDate.flatten()

编辑#2

或者您可以实施搜索:

List findKeys( map, key ) {
    map.findResults { k, v ->
        if( k == key ) v
        else if( v instanceof Map ) findKeys( v, key )
        else if( v instanceof Collection ) v.collectMany { findKeys( it, key ) }
        else null
    }.flatten()
}

assert findKeys( data, 'PeriodEndDate' ) == ['2013-10-10', '2012-10-10']