生成JSON基于映射键名称。

huangapple go评论64阅读模式
英文:

Generate JSON based on map keys names

问题

我是新手学习Groovy,有一个任务。我有一个看起来像这样的Map:

[sessionCampaignName:renins_KASKO_May23, sessionMedium:Yandex_banners, sessionSource:Media]
或者
[sessionCampaignName:olv_May23, sessionTerm:greatjob]

如何基于地图键名构建一个JSON,再加上一个名为date的额外JSON属性(例如,让我们以第一个例子为例):

[
        {
            "name": "date"
        },
        {
            "name": "sessionCampaignName"
        },
        {
            "name": "sessionMedium"
        },
        {
            "name": "sessionSource"
        }
    ]

尝试过以下方法:

List dimensionsList = convertedMap.collect { dimensionName, dimensionValue ->
        [
            [
                name: 'date'
            ],
            [
                name: dimensionName
            ]
        ]
      }
String jsonDimensions = JsonOutput.toJson(dimensionsList)

但没有达到我预期的效果。有什么建议吗?

英文:

I'm new to Groovy and I've a task. I have a Map looks like:

[sessionCampaignName:renins_KASKO_May23, sessionMedium:Yandex_banners, sessionSource:Media]
or
[sessionCampaignName:olv_May23, sessionTerm:greatjob]

How can i build a JSON based on map keys names + additional json property called date like (for example lets take 1st case):

[
        {
            "name": "date"
        },
        {
            "name": "sessionCampaignName"
        },
        {
            "name": "sessionMedium"
        },
        {
            "name": "sessionSource"
        }
    ]

Tried with:

List dimensionsList = convertedMap.collect {dimensionName, dimensionValue ->
        [
			[
            	name: 'date'
        	],
			[
				name: dimensionName
			]
		]
      }
String jsonDimensions = JsonOutput.toJson(dimensionsList)

But didn't reach what I expected to reach. Any tips?

答案1

得分: 2

尝试如下-

def map = [sessionCampaignName: 'renins_KASKO_May23', sessionMedium: 'Yandex_banners', sessionSource: 'Media']

List dimensionsList = [['name': 'date']] + map.keySet().collect { key ->
    ['name': key]
}

/* 
List dimensionsList = map.keySet().collect([name: 'date']) { key ->
    [name: key]
}
*/

String jsonDimensions = new groovy.json.JsonBuilder(dimensionsList).toPrettyString()
println jsonDimensions

输出:

[
    {
        "name": "date"
    },
    {
        "name": "sessionCampaignName"
    },
    {
        "name": "sessionMedium"
    },
    {
        "name": "sessionSource"
    }
]
英文:

Try as below-

def map = [sessionCampaignName: 'renins_KASKO_May23', sessionMedium: 'Yandex_banners', sessionSource: 'Media']

List dimensionsList = [['name': 'date']] + map.keySet().collect { key ->
    ['name': key]
}

/* 
List dimensionsList = map.keySet().collect([[name: 'date']]) { key ->
    [name: key]
}
*/

String jsonDimensions = new groovy.json.JsonBuilder(dimensionsList).toPrettyString()
println jsonDimensions

Output:

[
        {
            "name": "date"
        },
        {
            "name": "sessionCampaignName"
        },
        {
            "name": "sessionMedium"
        },
        {
            "name": "sessionSource"
        }
    ]

huangapple
  • 本文由 发表于 2023年6月1日 00:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定