按地图分组在Groovy [英] Group by Map in Groovy
问题描述
[
programs: [
trip: [
[
description: "",
tripName: "Niagra",
scheduleProgramTime: "2000-01-01T00:00:00-05:00",
country: "USA"
],
[
description: "",
tripName: "Chennai",
scheduleProgramTime : "2000-01-01T00:00:00-05:00",
country: "India"
],
[
description: "",
tripName: "Niagra",
scheduleProgramTime: "2000-01-01T00:00:00-05:00",
country: "USA"
]
],
trip: [ // Editor note: this line is in error. a map cannot have two properties named "trip". In practice this compiles, but when run the SECOND "trip" wipes out the FIRST. -- @BalRog
[
description: "",
tripName: "South Africa",
scheduleProgramTime: "2000-01-01T00:00:00-05:00",
country: "Africa"
],
[
description: "",
tripName: "Chennai",
scheduleProgramTime: "2000-01-01T00:00:00-05:00",
country: "India"
]
]
]
]
这是我的地图,我想基于tripName(Niagra / Chennai /南非)groupBy。输出我需要是像
This is my Map and I am trying to groupBy based on the tripName(Niagra/Chennai/South Africa). Output I need is like
[
"Niagra": [
[
description: "",
tripName: "Niagra",
scheduleProgramTime: "2000-01-01T00:00:00-05:00"
country: "USA"
],
[
description: "",
tripName: "Niagra",
scheduleProgramTime: "2000-01-01T00:00:00-05:00"
country: "USA"
]
],
"Chennai": [
[
description: "",
tripName: "Chennai",
scheduleProgramTime: "2000-01-01T00:00:00-05:00"
country: "India"
]
],
"South Africa": [
[
description: "",
tripName: "South Africa",
scheduleProgramTime: "2000-01-01T00:00:00-05:00"
country: "Africa"
]
]
]
这个 cityMap.trip.groupBy({it.tripName})
但没有得到正确的输出。
提前感谢。
I tried using this cityMap.trip.groupBy({it.tripName})
but not getting the proper output.
Thanks in advance.
我已将它更改为Map。现在以前我已经给了大括号,而不是[]大括号。现在根据地图,你可以提供我的详细信息。
I have changed it to Map. Now previously I have given curly braces instead of "[]" braces.Now based on the Map can you please provide me the details.
推荐答案
首先,从一个正确的Groovy Map
开始。您的示例不是Groovy Map
,但它看起来像JSON。如果是,您可以使用 JsonSlurper 进行解析获得地图
-like。 然后可以在旅行对象上使用 groupBy()
:
First, start with a proper Groovy Map
. Your example is not a Groovy Map
, however it does look like JSON. If so, you can parse it with JsonSlurper to get something Map
-like. Then you can use groupBy()
on the trip object:
def json = new JsonSlurper().parseText("""{
"trip": [
{
"description": "",
"tripName": "Niagra",
"scheduleProgramTime": "2000-01-01T00:00:00-05:00",
"country" : "USA"
},
{
"description": "",
"tripName": "Chennai",
"scheduleProgramTime": "2000-01-01T00:00:00-05:00",
"country" : "India"
},
{
"description": "",
"tripName": "Niagra",
"scheduleProgramTime": "2000-01-01T00:00:00-05:00",
"country" : "USA"
}
]
}
""")
json.trip.groupBy { it.tripName }
输出如下所示:
[
'Niagra': [
['country':'USA', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Niagra'],
['country':'USA', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Niagra']
],
'Chennai':[
['country':'India', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Chennai']
],
]
这篇关于按地图分组在Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!