两种方法:
1.用Ok("json")注解(返回json字符串)
<pre name="code" class="java"><pre name="code" class="java"> @At("/getMessageJson")
@Ok("json")
public String getMessageJson(){
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("name","张三");
resultMap.put("age", "22");
return Json.toJson(resultMap);
}2.用UTF8JsonView(返回json对象)
@At("/getMessageJson")
public View getMessageJson(){
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("name","张三");
resultMap.put("age", 22);
UTF8JsonView jsonView = new UTF8JsonView(new JsonFormat());
jsonView.setData(map);
return jsonView;
}
本文介绍了在Web开发中通过两种不同的方式来返回JSON数据:一种是使用Ok('json')注解直接返回JSON字符串;另一种则是利用UTF8JsonView返回JSON对象。这两种方法各有特点,能够满足不同的应用场景。
681

被折叠的 条评论
为什么被折叠?



