最近要写接口返回json报文数据 ,项目框架用的是spring、struts2对象返回的json网上查了下相关资料,现在把相关方法写一下,如果各位大神有看到文章有什么写的不妥的地方请指出来,小弟会及时更改。可能包含网上的一些资料
现在主要说两种方法:
1.使用servlet的输出流
public void json() throws IOException{HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
//response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String jsonString="{"code":"1","data":"","message":"注册成功","success":true}";
out.println(jsonString);
out.flush();
out.close();
}
这块可以写一个对象生成json的帮助类来生成json输出就好
然后配置xml
<action name="json" class="objectInformaTion" method="json" />
然后输出
{"code":&