SpringMvc-json处理
在 JSON 中,使用以下两种方式来表示数据:
Object(对象):键/值对(名称/值)的集合,使用花括号{ }定义。在每个键/值对中,以键开头,后跟一个冒号:,最后是值。多个键/值对之间使用逗号,分隔,例如{“name”:“C语言中文网”,“url”:“http://c.biancheng.net”};
Array(数组):值的有序集合,使用方括号[ ]定义,数组中每个值之间使用逗号,进行分隔。
示例:
{ "Name":"芜湖", "Id":9 "Article":[ "JSON 是什么?", "JSONP 是什么?", "JSON 语法规则" ] }
简单了解了 JSON 之后,我们再来看看什么时候适合使用 JSON。 定义接口 JSON 使用最多的地方莫过于 Web 开发领域了,现在的数据接口基本上都是返回 JSON 格式的数据,比如:
1.使用 Ajax 异步加载的数据;
2.RPC 远程调用;
3.前后端分离,后端返回的数据;
4.开发 API,例如百度、高德的一些开放接口。
1.响应json
·响应json
1 加入jackson依赖
2 将jackson 的jar包加入WEB-INF的lib文件夹 *
3.在对应处理方法上面加上@ResponseBody用于标记该处理方法返回json
将@Controller改成@RestController
在pom.xml中加入jackson相关依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.13.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.13.3</version> </dependency>
将依赖放入对应的文件夹中
通过在方法上加入@RespinseBody可以将返回值以文本形式输出
或者将类上面添加@RestController,即这个类下面方法输出的都是文本形式
@RequestMapping("/json/response") @ResponseBody public User Jsonresponse(){ User user = new User(5,"芜湖"); return user; }
对于一些私密变量不想让别人看见 或者 一些特定日期类型 可以使用json中特定的方法
@JsonIgnore //隐藏值 private String password; @JsonFormat(pattern = "yyyy-MM-dd") //规范格式 private Date birthday;
2.请求json
ajax我们经常用到,传的数据是json数据,json数据又有对象,数组。以下给大家总结了4 种常用的获取json方式:
前端:
<%-- Created by IntelliJ IDEA. User: 小bobo Date: 2022/9/22 Time: 14:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> <script src = "https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script> <script type="text/javascript"> //页面加载事件简写方式 $(function () { $("#btnJson1").click(function () { $.ajax({ url:"${pageContext.request.contextPath}/json/response2", method:"post", data:"张三", contentType:"application/json", dataType:"json", success:function (user) { alert(user.name); } }); }); $("#btnJson2").click(function(){ var user={'id':'1','name':'张三'}; // 定义js对象 $.ajax({ url:"${pageContext.request.contextPath}/json/wuhu", method:"post", data:JSON.stringify(user), contentType:'application/json', dataType:"json", success:function(user){ alert(user.name); } }); }); $("#btnJson3").click(function(){ // var user={'id':'1','name':'张三'}; // 定义js对象 // var jsonValue=JSON.stringify(user); // 对象转换为json字符串 // console.log(jsonValue) $.ajax({ url:"${pageContext.request.contextPath}/json/wuhu1", method:"post", data:'{"id":"1","name":"张三","birthday":"2019-01-01"}', contentType:'application/json', dataType:"json", success:function(user){ alert(user.name); } }); }); $("#btnJson4").click(function(){ var listUser=new Array(); var user1={"id":"1","name":"张三","birthady":"2019-01-01"}; var user2={"id":"2","name":"李四","birthady":"2019-01-01"}; listUser.push(user1) listUser.push(user2) $.ajax({ url:"${pageContext.request.contextPath}/json/wuhu2", method:"post", data:JSON.stringify(listUser), contentType:'application/json', dataType:"json", success:function(user){ alert(user.name); } }); }); }) </script> </head> <body> <input type="button" value="发送单个参数json数据" id = "btnJson1"/> <input type="button" value="发送对象的json数据用javabean接收" id="btnJson2"/> <input type="button" value="发送对象的json数据用javabean接收" id="btnJson3"/> <input type="button" value="发送对象的json数据用javabean接收" id="btnJson4"/> </body> </html>
后端
@Controller public class JsonController { @RequestMapping("/json/response") @ResponseBody public User Jsonresponse(){ User user = new User(5,"芜湖"); return user; } @RequestMapping("/json/haha") @ResponseBody public User Jsonresponse1(){ User user = new User(6,"haha","asfdl",new Date()); return user; } 1.以RequestParam接收 @RequestMapping(value = "/json/response2") @ResponseBody public User Jsonresponse2(@RequestBody String name){ User user2 = new User(2,"haha","1215122",new Date()); System.out.println(name); return user2; } 2.javabean @PostMapping(value = "/json/wuhu",consumes = "application/json") @ResponseBody public User Jsonresponse3(@RequestBody User user){ User user2 = new User(2,"haha","1215122",new Date()); System.out.println(user); return user2; } 3.map集合 @PostMapping(value = "/json/wuhu1",consumes = "application/json") @ResponseBody public User Jsonresponse4(@RequestBody Map<String,String> map){ User user2 = new User(2,"haha","1215122",new Date()); System.out.println(map); return user2; } 4.列表 @PostMapping(value = "/json/wuhu2",consumes = "application/json") @ResponseBody public User Jsonresponse5(@RequestBody List<User> list){ User user2 = new User(2,"haha","1215122",new Date()); System.out.println(list); return user2; } }