playframework .json模板List-Ojects与foreach? [英] playframework .json template List-Ojects with foreach?
问题描述
我的问题是:我不想在json响应中返回整个模型对象的结构和数据. renderJSON()从响应中返回模型中的所有内容.
My Problem is: I don't want to return the whole model object structure and datas in a json response. renderJSON() returns everything from the model in the response.
所以我认为最好的方法是使用* .json模板.好的,现在我有了一个列表,现在我不再需要* .json-template中的语法了.
So I thought the best way would be to use *.json templates. Ok and now I have a List and I don't now how the syntax in the *.json-template must be.
控制器的一部分:
List<User> users = User.find("byActive", true).fetch();
if (users != null) {
render(users);
}
"User/showAll.json"(模板):
"User/showAll.json" (template):
//something like foreach( User currentUser in users )
{
"username": "${currentUser.name}",
"userdescr": "${currentUser.description}"
}
对于单个用户没问题,我明白了,但是json模板中的用户列表看起来如何?
For a single user it's no problem, I got it, but how does it look like for a List of users inside the json template?
推荐答案
[
#{list users}
{
"username": "${_.name}",
"userdescr": "${_.description}"
} #{if !_isLast},#{/if}
#{/list}
]
检查 http://www.playframework.org/documentation/1.2.4 /tags#list 了解更多信息
这篇关于playframework .json模板List-Ojects与foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!