Groovy的简单JSON数组建设者 [英] Groovy Simple JSON array builder

查看:916
本文介绍了Groovy的简单JSON数组建设者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个JSON简单的JSON数组,但在循环它的每一个迭代过程中覆盖了第一个值。

I need to build a simple JSON array in JSON but in the loop it overwrites the first value during every iteration.

def jsonBuilder = new groovy.json.JsonBuilder()
contact.each {
            jsonBuilder.contact(
                    FirstName:  it.getFirstName(),
                    LastName:  it.getLastName(),
                    Title: it.getTitle(),       
            )
    }

它返回只是简单的JSON,并覆盖每个迭代的值,并只保留最后一个。什么是在Groovy构建JSON数组的语法?

It returns just the simple JSON and overwrites the value of every iteration and retains just the last one. What is the syntax for constructing a JSON Array in groovy?

推荐答案

诀窍就是从联系人列表中收集。假设合约列表的结构如下,要走的路 jsonBuilder 如下使用。

Trick is to collect from the list of contacts. Assuming structure of contract list is as below, follow the way jsonBuilder is used below.

def contact = [ 
    [ getFirstName : { 'A' }, getLastName : { 'B' }, getTitle : { 'C' } ], 
    [ getFirstName : { 'D' }, getLastName : { 'E' }, getTitle : { 'F' } ], 
    [ getFirstName : { 'G' }, getLastName : { 'H' }, getTitle : { 'I' } ]     
]

def jsonBuilder = new groovy.json.JsonBuilder()

jsonBuilder {
    contacts contact.collect { 
        [ 
            FirstName: it.getFirstName(), 
            LastName: it.getLastName(), 
            Title: it.getTitle() 
        ] 
    }
}

println jsonBuilder.toPrettyString()


// Prints
{
    "contacts": [
        {
            "FirstName": "A",
            "LastName": "B",
            "Title": "C"
        },
        {
            "FirstName": "D",
            "LastName": "E",
            "Title": "F"
        },
        {
            "FirstName": "G",
            "LastName": "H",
            "Title": "I"
        }
    ]
}

如果你正在寻找一个JSONArray,而不是一个JSONObject作为最后stucture,然后用:

If you are looking for a JSONArray instead of a JSONObject as a final stucture, then use:

jsonBuilder(
    contact.collect { 
        [ 
            FirstName: it.getFirstName(), 
            LastName: it.getLastName(), 
            Title: it.getTitle() 
        ]
    }
)

// OP
[
    {
        "FirstName": "A",
        "LastName": "B",
        "Title": "C"
    },
    {
        "FirstName": "D",
        "LastName": "E",
        "Title": "F"
    },
    {
        "FirstName": "G",
        "LastName": "H",
        "Title": "I"
    }
]

这没有任何意义,但如果需要的结构像下面

It does not make sense but if structure needed like below

[
    {
        "contact": {
            "FirstName": "A",
            "LastName": "B",
            "Title": "C"
        }
    },
    {
        "contact": {
            "FirstName": "D",
            "LastName": "E",
            "Title": "F"
        }
    },
    {
        "contact": {
            "FirstName": "G",
            "LastName": "H",
            "Title": "I"
        }
    }
]

然后用

jsonBuilder(
    contact.collect { 
        [ 
            contact : [ 
                FirstName: it.getFirstName(), 
                LastName: it.getLastName(), 
                Title: it.getTitle() 
            ] 
        ]
    }
)

这篇关于Groovy的简单JSON数组建设者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆