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



正如标题所示,我正试图将一个JsonBuilder对象中包含的信息添加到另一个Json Builder对象中。

目前我有这个:

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
               ]
            }
        }

但这返回了除Holidays密钥外的所有密钥。

如有任何帮助,我们将不胜感激!

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

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 = []

}

度假物品活动:

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
}

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
}

不需要冒号:。请参阅此处的示例。而且我没有Joda依赖项,所以用String代替了它。

相关内容

  • 没有找到相关文章

最新更新