介绍
这篇文章给大家分享的是有关java返回json的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
<强>方式1:当然是手敲所有代码,来进行json数据的返回。强>
需要,HttpHttpServletRequest请求,HttpServletResponse响应
后台:
@RequestMapping (value=https://www.yisu.com/zixun//哈哈) 公共空xxx (HttpHttpServletRequest请求,HttpServletResponse响应) {JSONObject json=new JSONObject (); json。把(“结果”,“成功”) response.setCharacterEncoding(“utf - 8”); response.setContentType(“application/json; charset=utf - 8”); PrintWriter=零;=response.getWriter (); out.write (json.toString ()); }
前端:
. ajax({美元 数据:{//userNameOrTel: $ (“# user") .val (),//密码:$ (“# pwd") .val () }, 类型:“post", url:“admin/登录/? 数据类型:“json" contentType:“application/json; charset=utf-8" 异步:假的,//同步异步 成功:功能(数据){ 调试器; } } });
<强>方式2:,@ResponseBody注解强>
<强>后台:强>
@ResponseBody @RequestMapping (value=https://www.yisu.com/zixun//哈哈) 公共味精xxx(){返回味精}
<强>前端:强>
. ajax({美元 数据:{//userNameOrTel: $ (“# user") .val (),//密码:$ (“# pwd") .val () }, 类型:“post", url:“admin/登录/? 数据类型:“json" contentType:“application/json; charset=utf-8" 异步:假的,//同步异步 成功:功能(数据){ 调试器; } } });
<强>方式3:,@RestController注解(此类里的所以方法返回值都是Json) 强>
<强>前端:强>
数据:JSON.stringify ({& # 39; channelId& # 39;: channelId}), 成功:功能(数据){ 警报(data.channelId); }, contentType: & # 39; application/json; charset=utf - 8 # 39;
<强>后台:强>
@RequestMapping (value=https://www.yisu.com/zixun/?登录”,产生=癮pplication/json; charset=utf - 8”) @ResponseBody公共字符串test2 () {}
感谢各位的阅读!关于java返回json的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!