gpt4 book ai didi

java - 将 JSON Builder 与集合结合使用

转载 作者:行者123 更新时间:2023-12-01 12:10:24 24 4
gpt4 key购买 nike

我有这个代码:

    // Write metadate.json
def jsonBuilder = new groovy.json.JsonBuilder()
List myTemplateFilesList = new ArrayList();

def myTemplate = new MyTemplate(module: ''+module,
version: ''+version,
unit: ''+unit,
defaultPropertiesTargetPath: ''+targetPath)
myTemplateFilesList.add(myTemplate);
for (String key : keys) {
def myTemplateFiles = new MyTemplateFiles(templateName: key,
templateFileName: key+'.ht',
targetFileName: key+'{{env}}{{client}}')
myTemplateFilesList.add(myTemplateFiles);
}
jsonBuilder('templateFiles': myTemplateFilesList);
println(jsonBuilder.toPrettyString())

class MyTemplate{
def module
def version
def unit
def defaultPropertiesTargetPath
}

class MyTemplateFiles{
def templateName
def templateFileName
def targetFileName
}

我的输出:

{
"templateFiles": [
{
"unit": "WAS-WDI",
"version": "20_2",
"defaultPropertiesTargetPath": "defaultPropertiesTargetPath:{{user.home}}/{{component}}/deployments/{{module}}/properties",
"module": "wdiCore"
},
{
"templateFileName": "EnvWDI.ht",
"templateName": "EnvWDI",
"targetFileName": "EnvWDI{{env}}{{client}}"
},
{
"templateFileName": "WDIHealthCheck.ht",
"templateName": "WDIHealthCheck",
"targetFileName": "WDIHealthCheck{{env}}{{client}}"
},
{
"templateFileName": "CachesWDI.ht",
"templateName": "CachesWDI",
"targetFileName": "CachesWDI{{env}}{{client}}"
},
{
"templateFileName": ".ht",
"templateName": "",
"targetFileName": "{{env}}{{client}}"
}
] }

但我想要这个输出:

{
"module": "wdiCore",
"version": "20.1.1.0",
"unit": "WAS-WDI",
"defaultPropertiesTargetPath":"{{user.home}}/{{component}}/deployments/{{module}}/properties",
"templateFiles" :
[
{
"templateName": "EnvWDI" ,
"templateFileName": "EnvWDI.ht" ,
"targetFileName": "EnvWDI{{env}}{{client}}"
},
{
"templateName": "WDIHealthCheck{env}{client}" ,
"templateFileName": "WDIHealthCheck.ht" ,
"targetFileName": "WDIHealthCheck{{env}}{{client}}"
},
{
"templateName": "CachesWDI" ,
"templateFileName": "CachesWDI.ht" ,
"targetFileName": "CachesWDI{{env}}{{client}}",
"targetPath":"{{user.home}}/{{component}}/properties/cache"
}
]

}

我的代码有什么问题..?

最佳答案

首先,我必须假设您所需的输出部分包含拼写错误。我期望以下行:

      "templateName": "WDIHealthCheck{env}{client}" ,

实际上应该是:

      "templateName": "WDIHealthCheck" ,

对吗?

解决方案 1:定义 Groovy 对象以反射(reflect) JSON 结构

基本上,您只需使 Groovy 类看起来像您想要生成的 JSON 即可。那么让我们从顶部开始。您希望 JSON 对象的顶层包含 moduleversionunitdefaultPropertiesTargetPath 和列表templateFiles。让我们在 Groovy 中定义该结构:

class MyTemplate{
def module
def version
def unit
def defaultPropertiesTargetPath
List templateFiles
}

现在,该列表中有更多对象,所有对象都具有大致相同的结构。让我们在 Groovy 中定义该结构:

class MyTemplateFiles {
def templateName
def templateFileName
def targetFileName
}

但这还不是全部,因为有一个带有附加属性的特殊情况:

class MySpecialTemplateFiles extends MyTemplateFiles {
def targetPath
}

然后您的脚本代码如下所示(记住将 targetPath 属性添加到特殊情况):

List myTemplateFilesList = new ArrayList()
def myTemplate = new MyTemplate(
module: module,
version: version,
unit: unit,
defaultPropertiesTargetPath: targetPath)
// add the list to the template, not the other way around
myTemplate.templateFiles = myTemplateFilesList
for (String key : keys) {
def myTemplateFiles
// isolates special case
if (key == "CachesWDI") {
myTemplateFiles = new MySpecialTemplateFiles(
templateName: key,
templateFileName: key+'.ht',
targetFileName: key+'{{env}}{{client}}',
targetPath: "{{user.home}}/{{component}}/properties/cache")
} else {
myTemplateFiles = new MyTemplateFiles(
templateName: key,
templateFileName: key+'.ht',
targetFileName: key+'{{env}}{{client}}')
}
myTemplateFilesList.add(myTemplateFiles)
}
// use template to instantiate jsonBuilder
def jsonBuilder = new groovy.json.JsonBuilder(myTemplate)
println(jsonBuilder.toPrettyString())

这会产生以下输出:

{
"unit": "WAS-WDI",
"version": "20.1.1.0",
"defaultPropertiesTargetPath": "{{user.home}}/{{component}}/deployments/{{module}}/properties",
"templateFiles": [
{
"templateFileName": "EnvWDI.ht",
"templateName": "EnvWDI",
"targetFileName": "EnvWDI{{env}}{{client}}"
},
{
"templateFileName": "WDIHealthCheck.ht",
"templateName": "WDIHealthCheck",
"targetFileName": "WDIHealthCheck{{env}}{{client}}"
},
{
"targetPath": "{{user.home}}/{{component}}/properties/cache",
"templateFileName": "CachesWDI.ht",
"templateName": "CachesWDI",
"targetFileName": "CachesWDI{{env}}{{client}}"
}
],
"module": "wdiCore"
}

JSON 对象中的属性是无序的,因此这相当于您想要的输出。

解决方案 2:通过 JsonBuilder DSL 定义 JSON

无需在 Groovy 中定义类,而是使用 JsonBuilder 领域特定语言 (DSL) 直接定义 JSON 对象。在下面,我重命名了一些假定的输入,以避免命名冲突(module => myModuleunit => myUnit 等):

def jsonBuilder = new groovy.json.JsonBuilder()
jsonBuilder {
module myModule
version myVersion
unit myUnit
defaultPropertiesTargetPath myTargetPath
// the following produces a list of maps for the templateFiles property
templateFiles keys.collect { key ->
def map = [
templateName: key,
templateFileName: key+'.ht',
targetFileName: key+'{{env}}{{client}}']
if (key == "CachesWDI") {
map.targetPath = "{{user.home}}/{{component}}/properties/cache"
}
map
}
}
println(jsonBuilder.toPrettyString())

该程序会产生您想要的确切输出。

关于java - 将 JSON Builder 与集合结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com