如何使用Groovy JsonBuilder生成带有嵌套同级对象的对象数组

时间:2019-12-17 01:06:58

标签: json 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())

1 个答案:

答案 0 :(得分:2)

就像评论中提到的那样,以我的经验,最好保持groovy中的列表和地图,并仅将其转换为json作为最后一步。这样,您就可以利用所有常规特性来处理地图和列表(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之类的东西时)时,您不需要引号。如果遇到破坏上述模式的键,则始终可以引用这些键。