我正在写一个Play应用程序,它将有数十个控制器操作返回JSON。每个JSON结果的格式稍有不同,由几个基元组成。在Play中,渲染JSON时可以使用匿名类型和/或对象初始值设定项吗?
我想避免创建一个Java类,以保持每个动作方法的返回类型,所以目前我使用一个HashMap,像这样:
// used to populate the filters for an iphone app
public static void filters()
{
// get four lists from the database
List<Chef> chefs= Chef.find("order by name asc").fetch();
List<Cuisine> cuisines = Cuisine.find("order by name asc").fetch();
List<Meal> meals = Meal.find("order by name asc").fetch();
List<Ingredient> ingredients = Ingredient.find("order by name asc").fetch();
// return them as JSON map
Map<String,Object> json = new HashMap<String,Object>();
json.put("chefs", chefs);
json.put("cuisines", cuisines);
json.put("meals", meals);
json.put("ingredients", ingredients);
renderJSON(json);
}
这将返回JSON看起来像这样,这就是我想:
{
"chefs": [{},{},...{}],
"cuisines": [{},{},...{}],
"meals": [{},{},...{}],
"ingredients": [{},{},...{}]
}
我觉得的语法来构建的HashMap是多余的。我没有一吨的Java的经验,所以我比较C#这让我使用匿名类型与对象初始化,以减少的代码如下所示:
return Json(new
{
chefs = chefs,
cuisines = cuisines,
meals = meals,
ingredients = ingredients
});
Java/Play世界里有什么让我更加紧凑地写这种代码的吗?
不幸的是,那只是返回一个空字符串。 \t \t renderJSON(new HashMap(){{put(“foo”,“string”); put(“bar”,123); }}); –
Portman
有趣。看起来renderJSON中的反射魔术不处理地图的子类。从匿名图创建实际的HashMap确实有效:\t Map map = new HashMap (){{put(“foo”,“string”);放(“酒吧”,123); }}; renderJSON(new HashMap (map)); –
我做了一些另外的测试,并且renderJSON处理了HashMap的普通子类。所以只有匿名的子类会导致问题。 –