gpt4 book ai didi

json - Grails JSON渲染

转载 作者:行者123 更新时间:2023-12-02 14:52:13 25 4
gpt4 key购买 nike

我使用Grails 2.3.8。我注意到Grails JSON呈现中的奇怪行为。我有一个映射到mongodb的域类ShiftUpdateResponse,如下所示。

     class ShiftUpdateResponse {
List<Employee> employees
SalesPersonHours salesPersonHours;
Integer fromDate
Integer toDate
Integer success

static mapWith = "mongo"
static hasMany = [employees:Employee]
static embedded = ['employees','salesPersonHours']
static constraints = {
salesPersonHours nullable:true
success nullable:true
fromDate nullable:true
toDate nullable:true
}

String toString() {
" $employees"
}
}

当我在 Controller 中创建 ShiftUpdateResponse对象并使用以下代码段将其呈现为JSON时:
   def  sur = new ShiftUpdateResponse(employees:[], salesPersonHours:[], success:1, fromDate:from, toDate:to)
println "from " + from
println "to " + to
println "from sur " + sur.fromDate
println "to sur " + sur.toDate
JSON.use('deep')
render sur as JSON

我注意到以下几点
  • 当我在渲染前不保存创建的实例sur时,控制台输出为
    from 20130624
    to 20130623
    from sur null
    to sur null
  • 当我在渲染之前保存创建的实例时,控制台输出为
    from 20130624
    to 20130623
    from sur null
    to sur 20130623

  • 无论哪种情况,呈现的JSON都不包含 fromDatetoDate字段。可能是什么原因 ? (我不需要保留该实例,只需要将其呈现为JSON)。

    编辑:

    呈现的JSON如下所示:
        {
    "class": "com.scheduling.json.week.graphicalMode.ShiftUpdateResponse",
    "id": 37,
    "employees": [],
    "salesPersonHours": null,
    "success": 1
    }

    Controller 代码:
        @Transactional
    def updateShift(){

    def reqJson = request.JSON
    somService.updateShift(reqJson)


    def from
    def to
    def week = WeekJson.get(reqJson.week_id)

    // code to construct from and to from week

    def sur = new ShiftUpdateResponse(employees:[],salesPersonHours:[],success:1,fromDate:from,toDate:to)

    //sur.save(flush:true)

    println "from "+from
    println "to "+to
    println "from sur "+sur.fromDate
    println "to sur "+sur.toDate


    JSON.use('deep')
    render sur as JSON

    }

    最佳答案

    对不起,菜鸟错误。问题可能是因为在不同的程序包中存在相同文件的另一个副本。删除重复的固定东西。

    关于json - Grails JSON渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24574375/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com