我使用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都不包含
fromDate
和toDate
字段。可能是什么原因 ? (我不需要保留该实例,只需要将其呈现为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/