【问题标题】:How to use Grails JSON view without defining internal templates?如何在不定义内部模板的情况下使用 Grails JSON 视图?
【发布时间】:2017-11-18 12:48:00
【问题描述】:

我有一个域类:

class Business {
    String name
    String description
}

我有以下 JSON 模板:

index.gson: 为对象列表生成 JSON

_business.gson: 为业务对象生成 JSON

index.gson

import server.Business
model {
    Iterable businessList
}
json {
    result tmpl.business(businessList ?: [])
}

_business.gson

model {
        Business business
}
json {
    id business.id
    name business.name
} 

如何在不使用 _business.gson 模板的情况下为业务对象生成 JSON?

我想采用一种只有 index.gson 并手动渲染内部对象的方法。

import server.Business
model {
    Iterable businessList
}

json {
    **WHAT SHOULD I ADD HERE?**
}

json(businessList.toList()) {
    **I also noticed that I can use this syntax, BUT WHAT SHOULD I ADD HERE?**
}

【问题讨论】:

    标签: json grails grails3 grails3.2.0 json-view


    【解决方案1】:

    你可以在 json 闭包中做任何你想做的事情。

    json(businessList.toList()) { Business business ->
        id business.id
        name business.name
        description business.description
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多