0

我正在尝试生成一个包含多个嵌套对象的 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())
4

1 回答 1

2

就像评论中提到的那样,根据我的经验,最好保留 groovy 中的列表和地图,并且仅在最后一步转换为 json。通过这种方式,您可以使用处理地图和列表(、、、等)的所有常规优势collectfindAll改变groupBy您的数据,然后作为最后一步生成您的 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)时,您不需要引号。如果您遇到破坏上述模式的键,您可以随时引用这些键。

于 2019-12-17T09:23:52.087 回答