SpringMVC json处理 你的名字 2021-06-24 13:58 436阅读 0赞 ### json ### * 什么是JSON? * * JSON 和 JavaScript 对象互转 * Json处理 * * jackson * 导入依赖 * 使用@ResponseBody * 使用@RestController * 使用@RequestBody * jackson常用注解 * * 日期格式化 * 修改属性名 * 属性忽略 * null 和empty属性排除 * 自定义序列化 * FastJson * * 安装FastJson * 使用 * 常用注解 # 什么是JSON? # SON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛。 采用完全独立于编程语言的文本格式来存储和表示数据。 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率 JSON 是 JavaScript 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串 var obj = { a: 'Hello', b: 'World'}; //这是一个对象,注意键名也是可以使用引号包裹的 var json = '{"a": "Hello", "b": "World"}'; //这是一个 JSON 字符串,本质是一个字符串 ## JSON 和 JavaScript 对象互转 ## 要实现从JSON字符串转换为JavaScript 对象,使用 JSON.parse() 方法: var obj = JSON.parse('{"a": "Hello", "b": "World"}'); //结果是 {a: 'Hello', b: 'World'} 要实现从JavaScript 对象转换为JSON字符串,使用 JSON.stringify() 方法: var json = JSON.stringify({ a: 'Hello', b: 'World'}); //结果是 '{"a": "Hello", "b": "World"}' # Json处理 # ## jackson ## ## 导入依赖 ## <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.2</version> </dependency> ## 使用@ResponseBody ## package com.blb.web; import com.blb.entity.User; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays; import java.util.List; @Component @RequestMapping("/json") public class JsonController { @RequestMapping("/test1") @ResponseBody //将handler的返回值,转化成json(jackson),并将json响应给客户端 //@ResponseBody 还可以用在handler的返回值上 public User test1(){ User user=new User(1,"dyk"); return user; } @RequestMapping("/test2") @ResponseBody public List<User> test2(){ User user=new User(1,"dyk"); User user1=new User(2,"cb"); List<User> users= Arrays.asList(user,user1); return users; } @RequestMapping("/test3") @ResponseBody //如果返回值已经是字符串,则不需要转json,直接以字符串响应给客户端 public String test3() { return "hello"; //加了@ResponseBody后返回的就不再是hello.jsp了,直接在页面显示hello } @RequestMapping(value="/test4",produces = "text/html;charset=utf-8") @ResponseBody public String test4() { return "你好"; //produces加了才能显示中文不乱码 } } ## 使用@RestController ## Controller类加了@RestController注解等价于在类中的每个方法都加上了@ResponseBody ## 使用@RequestBody ## @RequestBody接收json参数 public class User { private Integer id; private String name; } **ajax发送json** <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script src="${pageContext.request.contextPath}/js/jquery.js"></script> </head> <body> <input type="button" value="ajax" onclick="send_json()"> <script> function send_json() { var user={ id:1,name:"dyk"}; var userjson=JSON.stringify(user); //转化js对象成json $.ajax({ url:"${pageContext.request.contextPath}/json/test5", type:"post", data:userjson, contentType:"application/json", //声明请求参数的类型为json success:function (ret) { alert(ret) } }) } </script> </body> </html> @ResponseBody @RequestMapping("/test5") public String test5(@RequestBody User user){ //@ResponseBody将请求体中的json数据转换为java对象 System.out.println(user); return "ok"; } ## jackson常用注解 ## ### 日期格式化 ### 默认的日期会以毫秒值的形式显示出来 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public class User2 { private Integer id; private String name; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date birth; get/set } ### 修改属性名 ### public class User2 { @JsonProperty("new_id")//不在使用原属性名,而是"new_id" private Integer id; private String name; get/set } 输出json时{ "new_id":xx,"birth":xx} 默认输出的是属性名 ### 属性忽略 ### @JsonIgnore //生成json时忽略此属性 public class User2 { private Integer id; @JsonIgnore //生成json时忽略此属性 private String name; private Date birth; } 输出json时{ "id":xx,"birth":xx} ### null 和empty属性排除 ### jackson默认会输出null值的属性,如果不需要可以排除 @JsonInclude(JsonInclude.Include.NON_NULL)//null值 属性不输出 @JsonInclude(JsonInclude.Include.NON_EMPTY)//empty属性,不输出(空串,长度为0的集合,null值) public class User2 { private Integer id; @JsonInclude(JsonInclude.Include.NON_NULL)//若name=null 忽略此属性 private String name; @JsonInclude(JsonInclude.Include.NON_EMPTY)//若hobby的长度为0或等于null,忽略此属性 private List<String> hobby; get/set } ### 自定义序列化 ### @JsonSerialize(using = MySerializer.class)//使用MySerializer输出 MySerialize 类 package com.blb.serialize; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import java.math.BigDecimal; public class MySerializer extends JsonSerializer<Double> { @Override public void serialize(Double aDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { //将double salary的值 四舍五入 String number= BigDecimal.valueOf(aDouble).setScale(2,BigDecimal.ROUND_HALF_UP).toString(); //输出 jsonGenerator.writeNumber(number); } } public class User2 { private Integer id; @JsonSerialize(using = MySerializer.class)//使用MySerializer输出 private Double salary=1234.567;//在输出次属性时,希望保留两位,并且四舍五入 private Date birth; get/set 输出json时{ "id":xx,"birth":xx,"salary":1234.57} } ## FastJson ## <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> ### 安装FastJson ### 在mvc.xml的mvc:annotation-driven里面加入 <!-- 注解驱动--> <mvc:annotation-driven> <!-- 安装Fastjson转换器--> <mvc:message-converters register-defaults="false"> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <!-- 加入支持的媒体类型:返回contentType --> <property name="supportedMediaTypes"> <list> <!-- 这里顺序不能反,一定先写text/html,不然ie下会出现下载提示 --> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> ### 使用 ### @RestController @RequestBody @ResponseBody 使用方法没变 ### 常用注解 ### 日期格式化 @JSONField(format="yyyy-MM-dd HH:mm:ss") 属性名修改 @JSONField(name = "new_id") 忽略属性 @JSONField(serialize = false) 包含Null值 @JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue) 默认会忽略所有null值,有此注解会输出null值 @JSONField(serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty) null的string会输出为"" 等等 自定义序列化 @JSONField(serializeUsing = MySerializer2.class) package com.blb.serialize; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.ObjectSerializer; import org.springframework.core.serializer.Serializer; import java.io.IOException; import java.lang.reflect.Type; public class MySerializer2 implements ObjectSerializer { @Override public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException { double value=(Double)o;//salary的属性值 String text=value+"元";//salary后面拼元 jsonSerializer.write(text);//输出拼接后的内容 } } public class User3 { @JSONField(name = "new_id") private Integer id; @JSONField(serialize = false) private String name; @JSONField(format="yyyy-MM-dd HH:mm:ss") private Date birth; @JSONField(serializeUsing = MySerializer2.class) private Double salary; }
还没有评论,来说两句吧...