Grails - Initiating domain class using JSON

走远了吗. 提交于 2019-12-18 08:58:53

问题


I have this simple domain class:

class Settings {
static constraints = {
    uid(nullable: false, unique: true)
    person()
}

String uid
Map person
}

and a web UI that update the data using a json request:

{"uid":1234 , person:{"first_name" : "jhon" , "last_name" : "doe"}}

in the controller code:

def json = request.JSON;
def s = new Settings(json);

it seems that s.uid is being set however the s.person Map remains empty. What am I missing?


回答1:


You can do something like the following in your controller:

def json = request.JSON;
def s = new Settings(json);
s.person = json.person;

it's ugly, but the data binding doesn't seem to handle nested json




回答2:


If you want that to work you need to convert your structure to this:

{"uid":1234 , "person.first_name": "jhon" , "person.last_name": "doe"}



回答3:


If you add this line before instantiating Settings, it will bind recursively.

JSON.use('deep')


来源:https://stackoverflow.com/questions/15067379/grails-initiating-domain-class-using-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!