Grails - Initiating domain class using JSON

后端 未结 3 954
情歌与酒
情歌与酒 2020-12-21 06:18

I have this simple domain class:

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

String uid
Map person
}


        
相关标签:
3条回答
  • 2020-12-21 06:28

    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

    0 讨论(0)
  • 2020-12-21 06:43

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

    JSON.use('deep')
    
    0 讨论(0)
  • 2020-12-21 06:45

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

    {"uid":1234 , "person.first_name": "jhon" , "person.last_name": "doe"}
    
    0 讨论(0)
提交回复
热议问题