将信息从一个JsonBuilder对象添加到另一个

时间:2015-02-20 09:26:11

标签: json groovy jsonbuilder

正如标题所示,我正在尝试将一个JsonBuilder对象中保存的信息添加到第二个JsonBuilder对象。

目前我有这个:

public String buildOneUser(DyveUserDTO user)
{
    def userBuilder = new JsonBuilder()
        userBuilder user.collect { usr ->
            [
                    'Name': usr.userName,
                    'Allowance': usr.allowance,
                    'Total Holidays in Calendar': usr.totalHolidaysInCal,
                    'Holidays Booked': usr.numHolidaysBooked,
                    'Holidays Taken': usr.numHolidaysTaken,
                    'Holidays Remaining': usr.totalHolidaysLeft
            ]
        }

    def userHolidayBuilder = new JsonBuilder()
        userHolidayBuilder user.holidayEvents.collect { usr ->
            [
                    'Start Date': usr.startDate,
                    'End Date': usr.endDate,
                    'Days': usr.days
            ]
        }

    def userAndHolidays = userBuilder + userHolidayBuilder

    return userAndHolidays.toPrettyString()
}

user.holidayEvents是表示假期的对象列表,它可以是空的,也可以包含任意数量的对象。这使我对做类似的事情犹豫不决:

def userBuilder = new JsonBuilder()
        userBuilder user.collect { usr ->
            [
                    'Name': usr.userName,
                    'Allowance': usr.allowance,
                    'Total Holidays in Calendar': usr.totalHolidaysInCal,
                    'Holidays Booked': usr.numHolidaysBooked,
                    'Holidays Taken': usr.numHolidaysTaken,
                    'Holidays Remaining': usr.totalHolidaysLeft
                    'Holiday': usr.holidayEvents[0].startDate
                    'Holiday': usr.holidayEvents[0].endDate
                    'Holiday': usr.holidayEvents[0].days
            ]
        }

因为我只会得到我为其编写代码的假期。如果用户没有假期,它也会抛出异常,我告诉它usr.holidayEvents[1],因为它在列表范围之外。

我也尝试过这样的.collect嵌套

def userBuilder = new JsonBuilder()
        userBuilder {
           'Name' user.userName,
           'Allowance' user.allowance,
           'Total Holidays in Calendar' user.totalHolidaysInCal,
           'Holidays Booked' user.numHolidaysBooked,
           'Holidays Taken' user.numHolidaysTaken,
           'Holidays Remaining' user.totalHolidaysLeft,
           'Holidays' user.holidayEvents.collect{ evt ->
               [
                  'Start Date': evt.startDate,
                  'End Date': evt.endDate,
                  'Days': evt.days
               ]
            }
        }

但是这会返回除假日键以外的所有键。

非常感谢任何帮助!

编辑 - 我的代码现在看起来像这样:

public String buildOneUser(DyveUserDTO user)
{
    def userBuilder = new JsonBuilder()
        userBuilder user.collect { usr ->
            [
                    'Name': usr.userName,
                    'Allowance': usr.allowance,
                    'Total Holidays in Calendar': usr.totalHolidaysInCal,
                    'Holidays Booked': usr.numHolidaysBooked,
                    'Holidays Taken': usr.numHolidaysTaken,
                    'Holidays Remaining': usr.totalHolidaysLeft,
                    'Holidays': usr.holidayEvents.collect{ evt ->
                        [
                                'Start Date': evt.startDate,
                                'End Date': evt.endDate,
                                'Days': evt.days
                        ]
                    }
            ]
        }
}

编辑2 - 示例代码

致电方法:

public String buildOneUser(DyveUserDTO user)
{
    def userBuilder = new JsonBuilder()
    userBuilder {
        Name:
        user.userName
        Allowance:
        user.allowance
        TotalHolidaysInCalendar:
        user.totalHolidaysInCal
        HolidaysBooked:
        user.numHolidaysBooked
        HolidaysTaken:
        user.numHolidaysTaken
        HolidaysRemaining:
        user.totalHolidaysLeft
        Holidays:
        user.holidayEvents.collect { evt ->
            [
                    'Start Date': evt.startDate,
                    'End Date'  : evt.endDate,
                    'Days'      : evt.days
            ]
        }
    }

    return userBuilder.toPrettyString()
}

传入的用户:

class DyveUserDTO
{
    String firstName = "Foo"
    String userName = "FooBar"
    Integer userID = 42
    BigDecimal numHolidaysBooked = 3
    BigDecimal numHolidaysTaken = 0
    BigDecimal totalHolidaysInCal = 3
    BigDecimal totalHolidaysLeft = 12
    BigDecimal allowance = 12
    List<HolidayObject> holidayEvents = []

}

HolidayEvents中的假日对象:

class HolidayObject
{
    public Integer userID = 42
    public String title = "Foo Holiday"
    public String event = "Holiday"
    public String amPm = "Full Day"
    public String name = "Foo"
    public LocalDateTime startDate = LocalDateTime.parse(2015-02-20T00:00:00)
    public LocalDateTime endDate = LocalDateTime.parse(2015-02-20T00:00:00)
    public BigDecimal days = 1
}

class HolidayObject
{
    public Integer userID = 42
    public String title = "Foo Holiday Pm"
    public String event = "Holiday"
    public String amPm = "Pm"
    public String name = "Foo"
    public LocalDateTime startDate = LocalDateTime.parse(2015-02-23T00:00:00)
    public LocalDateTime endDate = LocalDateTime.parse(2015-02-24T00:00:00)
    public BigDecimal days = 2
}

1 个答案:

答案 0 :(得分:0)

each只返回被调用的列表,collect应该用于events。请参阅以下工作代码:

import groovy.json.JsonBuilder

class UserEvent {
    def start
    def end
    def days
}

class User {
    def name
    def events
}

def u1 = new User(name: 'u1', events: [new UserEvent(start: 0, end: 1, days: 1), new UserEvent(start: 0, end: 2, days: 2)])
def u2 = new User(name: 'u2', events: [new UserEvent(start: 0, end: 3, days: 3)])

def users = [u1, u2]

def userBuilder = new JsonBuilder()
userBuilder users.collect { usr ->
   [
      'name': usr.name,   
      'events': usr.events.collect { e ->
         [
            start: e.start,
            end: e.end,
            days: e.days,
         ]
       }                   
    ]
}

print  userBuilder.toPrettyString()

修改

以下是一个工作示例:

import groovy.json.JsonBuilder

user = new DyveUserDTO()

def userBuilder = new JsonBuilder()
    userBuilder {
        Name user.userName
        Allowance user.allowance
        TotalHolidaysInCalendar user.totalHolidaysInCal
        HolidaysBooked user.numHolidaysBooked
        HolidaysTaken user.numHolidaysTaken
        HolidaysRemaining user.totalHolidaysLeft
        Holidays user.holidayEvents.collect { evt ->
            [
                    'Start Date': evt.startDate,
                    'End Date'  : evt.endDate,
                    'Days'      : evt.days
            ]
        }
    }

println userBuilder.toPrettyString()

class DyveUserDTO {
    String firstName = "Foo"
    String userName = "FooBar"
    Integer userID = 42
    BigDecimal numHolidaysBooked = 3
    BigDecimal numHolidaysTaken = 0
    BigDecimal totalHolidaysInCal = 3
    BigDecimal totalHolidaysLeft = 12
    BigDecimal allowance = 12
    List<HolidayObject> holidayEvents = [new HolidayObject(), new HolidayObject()]
}

class HolidayObject {
    public Integer userID = 42
    public String title = "Foo Holiday"
    public String event = "Holiday"
    public String amPm = "Full Day"
    public String name = "Foo"
    public String startDate = '2015-02-20T00:00:00'
    public String endDate = '2015-02-20T00:00:00'
    public BigDecimal days = 1
}

不需要冒号:。请参阅示例here。我也没有Joda依赖,所以用String替换。