如何使用 Groovy JsonBuilder 生成具有嵌套兄弟项的对象数组

How to Generate an Array of Objects with Nested Siblings with Groovy JsonBuilder

我正在尝试生成一个包含多个嵌套对象的 JSON 数组。

这是我想要生成的内容:(因为我想要一个数组,所以缩短了输出,如果您 运行 代码,这只是重复):

[
    {
        "User": {
            "Name": "Foo",
            "Email": "test@example.com"
        },
        "Details": {
            "Address": {
                "City": "Anywhere",
                "Country": "USA",
                "State": "ID",
                "ZipCode": "55842"
            },
            "FavoriteColor": "Blue"            
        }
    }
]

相反,我正在生成这个:

[
        {
            "User": {
                "Name": "Foo",
                "Email": "test@example.com"
            },
            "Address": {
                "City": "Anywhere",
                "Country": "USA",
                "State": "ID",
                "ZipCode": "55842"
            },
            "Details": [
                {
                    "FavoriteColor": "Blue"
                },
                {
                    "City": "Anywhere",
                    "Country": "USA",
                    "State": "ID",
                    "ZipCode": "55842"
                }
            ]
        }
    ]

这是我的代码:

def array = 1..3

def builder = new groovy.json.JsonBuilder()
builder array.collect { itemNumber ->
    [{
        User(
            Name: "Foo" + itemNumber,
            Email: "test@example.com"
        )
        Details(
            Address(
                City: "Anywhere",
                Country: "USA",
                State: "ID",
                ZipCode: "55842"
            ),
            FavoriteColor: "Blue"
        )
    }
    ]
}

println groovy.json.JsonOutput.prettyPrint(builder.toString())

如评论中所述,根据我的经验,最好保留 groovy 中的列表和地图,并且只在最后一步转换为 json。通过这种方式,您可以使用所有 groovy 优点来处理地图和列表(collectfindAllgroupBy 等)来改变您的数据,然后作为最后一步生成你的 json。

示例代码:

import groovy.json.JsonOutput

def numbers = 1..3

def data = numbers.collect { n -> 
  [
    User: [
      Name: "Foo${n}", 
      Email: "test@example.com"
    ],
    Details: [
      Address: [
        City:     "Anywhere", 
        Country:  "USA", 
        State:    "ID", 
        ZipCode:  "55842"
      ],
      FavoriteColor: "Blue"
    ]
  ]
}

def json   = JsonOutput.toJson(data)
def pretty = JsonOutput.prettyPrint(json)
println "JSON:\n${pretty}"

当 运行 时生成:

─➤ groovy solution.groovy
JSON:
[
    {
        "User": {
            "Name": "Foo1",
            "Email": "test@example.com"
        },
        "Details": {
            "Address": {
                "City": "Anywhere",
                "Country": "USA",
                "State": "ID",
                "ZipCode": "55842"
            },
            "FavoriteColor": "Blue"
        }
    },
    {
        "User": {
            "Name": "Foo2",
            "Email": "test@example.com"
        },
        "Details": {
            "Address": {
                "City": "Anywhere",
                "Country": "USA",
                "State": "ID",
                "ZipCode": "55842"
            },
            "FavoriteColor": "Blue"
        }
    },
    {
        "User": {
            "Name": "Foo3",
            "Email": "test@example.com"
        },
        "Details": {
            "Address": {
                "City": "Anywhere",
                "Country": "USA",
                "State": "ID",
                "ZipCode": "55842"
            },
            "FavoriteColor": "Blue"
        }
    }
]

关于 groovy 中地图键的说明,我没有在上面引用我的,因为当你的键是有效标识符时(即不像 Favourite-Color)你不需要引号。如果你 运行 进入打破上述模式的键,你总是可以引用这些键。