ToB企服应用市场:ToB评测及商务社交产业平台

标题: SpringMVC-05-Json [打印本页]

作者: 王國慶    时间: 2024-9-6 08:44
标题: SpringMVC-05-Json
1、什么是JSON?

JSON: JavaScript Object Notation(JS 对象描述法)。
JSON 是一种存储和交换数据的语法。
JSON 是通过 JS对象描述法书写的文本,用字面文本的形式来表示一个JS对象
2、为什么要使用JSON?

3、如何使用JSON?

在 JavaScript 语言中,一切都是对象。因此,任何JavaScript 支持的范例都可以通过 JSON 来表示,比方字符串、数字、对象、数组等。看看它的要求和语法格式:
JSON 和 JavaScript 对象的关系
  1. var obj = {a: 'Hello', b: 'World'}; //这是一个js对象,使用json语法构造,注意键名也是可以使用引号包裹的
  2. var json = '{"a": "Hello", "b": "World"}'; //这是一个 JSON字符串,符合json语法规则,本质是一个字符串
复制代码
JSON 和 JavaScript 对象互转
代码测试
4、在SpringMVC中使用JSON

这里我们主要使用两个第三方工具库:Jackson 与 FastJson
Jackson

底子使用

代码优化

启动tomcat测试,结果都正常输出!
测试集合输出

增加一个新的方法
  1.     @RequestMapping(value = "/json2")
  2.     public String json2() throws JsonProcessingException {
  3.         //创建一个jackson的对象映射器,用来解析数据
  4.         ObjectMapper mapper = new ObjectMapper();
  5.         //创建一个对象
  6.         User user1 = new User("用户1号", 4, "男");
  7.         User user2 = new User("用户2号", 3, "女");
  8.         User user3 = new User("用户3号", 2, "武装直升机");
  9.         User user4 = new User("用户4号", 1, "沃尔玛购物袋");
  10.         List<User> list = new ArrayList<>();
  11.         list.add(user1);
  12.         list.add(user2);
  13.         list.add(user3);
  14.         list.add(user4);
  15.         //将我们的对象解析成为json格式
  16.         String str = mapper.writeValueAsString(list);
  17.         System.out.println(str);
  18.         return str;
  19.     }
复制代码
运行结果 : 十分完美,没有任何问题!

输出时间对象

增加一个新的方法
  1.     @RequestMapping("/json3")
  2.     public String json3() throws JsonProcessingException {
  3.         //创建一个jackson的对象映射器,用来解析数据
  4.         ObjectMapper mapper = new ObjectMapper();
  5.         //创建时间一个对象,java.util.Date
  6.         Date date = new Date();
  7.         //将我们的对象解析成为json格式
  8.         String str = mapper.writeValueAsString(date);
  9.         System.out.println(str);
  10.         return str;
  11.     }
复制代码
运行结果 :

解决方案:取消timestamps形式 , 自界说时间格式
  1.     @RequestMapping("/json4")
  2.     public String json4() throws JsonProcessingException {
  3.         ObjectMapper mapper = new ObjectMapper();
  4.         //不使用时间戳的方式
  5.         mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  6.         //自定义日期格式对象
  7.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8.         //指定日期格式
  9.         mapper.setDateFormat(sdf);
  10.         Date date = new Date();
  11.         String str = mapper.writeValueAsString(date);
  12.         System.out.println(str);
  13.         return str;
  14.     }
复制代码
运行结果 : 成功的输出了相应的格式化时间!

抽取为工具类

如果要经常使用的话,这样是比较贫苦的,我们可以将这些代码封装到一个工具类中;我们去编写下
  1. package com.moondream.util;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.SerializationFeature;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. public class JsonUtils {
  8.     public static String getCommonJson(Object object) throws JsonProcessingException {
  9.         ObjectMapper objectMapper = new ObjectMapper();
  10.         return objectMapper.writeValueAsString(object);
  11.     }
  12.     public static String getDateJson(Date date) throws JsonProcessingException {
  13.         return getDateJson(date, "yyyy-MM-dd HH:mm:ss");
  14.     }
  15.     public static String getDateJson(Date date, String dateFormat) throws JsonProcessingException {
  16.         ObjectMapper objectMapper = new ObjectMapper();
  17.         objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  18.         objectMapper.setDateFormat(new SimpleDateFormat(dateFormat));
  19.         return objectMapper.writeValueAsString(date);
  20.     }
  21. }
复制代码
我们使用工具类,代码就更加简洁了!
  1.     @RequestMapping("/json5")
  2.     public String json5() throws JsonProcessingException {
  3.         Date date = new Date();
  4.         String json = JsonUtils.getDateJson(date);
  5.         System.out.println(json);
  6.         return json;
  7.     }
复制代码
高级使用

我们解决乱码问题和使用 Jackson 也可以在Spring体系内进行。
在 spring-mvc.xml 中,添加配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.        xsi:schemaLocation="
  7.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  9.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. ">
  11.     <context:component-scan base-package="com.moondream.controller"/>
  12.     <bean >
  13.         <property name="prefix" value="/WEB-INF/jsp"/>
  14.         <property name="suffix" value=".jsp"/>
  15.     </bean>
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xmlns:context="http://www.springframework.org/schema/context"
  20.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  21.        xsi:schemaLocation="
  22.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  23.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  24.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  25. ">
  26.     <context:component-scan base-package="com.moondream.controller"/>
  27.     <bean >
  28.         <property name="prefix" value="/WEB-INF/jsp"/>
  29.         <property name="suffix" value=".jsp"/>
  30.     </bean>
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xmlns:context="http://www.springframework.org/schema/context"
  35.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  36.        xsi:schemaLocation="
  37.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  38.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  39.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  40. ">
  41.     <context:component-scan base-package="com.moondream.controller"/>
  42.     <bean >
  43.         <property name="prefix" value="/WEB-INF/jsp"/>
  44.         <property name="suffix" value=".jsp"/>
  45.     </bean>
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:context="http://www.springframework.org/schema/context"
  50.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  51.        xsi:schemaLocation="
  52.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  53.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  54.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  55. ">
  56.     <context:component-scan base-package="com.moondream.controller"/>
  57.     <bean >
  58.         <property name="prefix" value="/WEB-INF/jsp"/>
  59.         <property name="suffix" value=".jsp"/>
  60.     </bean>
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xmlns:context="http://www.springframework.org/schema/context"
  65.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  66.        xsi:schemaLocation="
  67.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  68.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  69.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  70. ">
  71.     <context:component-scan base-package="com.moondream.controller"/>
  72.     <bean >
  73.         <property name="prefix" value="/WEB-INF/jsp"/>
  74.         <property name="suffix" value=".jsp"/>
  75.     </bean>
  76. </beans><?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.        xmlns:context="http://www.springframework.org/schema/context"
  80.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  81.        xsi:schemaLocation="
  82.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  83.        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  84.        http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
  85. ">
  86.     <context:component-scan base-package="com.moondream.controller"/>
  87.     <bean >
  88.         <property name="prefix" value="/WEB-INF/jsp"/>
  89.         <property name="suffix" value=".jsp"/>
  90.     </bean>
  91. </beans>               
复制代码
配置阐明:
注意:Spring每次需要使用消息转换器时,只会从消息转换列表中,挑选一个合适的消息转换器执行,多个消息转换器合适,则使用先注册的。
测试:
  1.     @RequestMapping("/json6")
  2.     public String json6() {
  3.         return "中文乱码测试";
  4.     }
  5.     @RequestMapping("/json7")
  6.     public User json7() {
  7.         User user = new User("用户1号", 3, "男");
  8.         return user;
  9.     }
  10.     @RequestMapping("/json8")
  11.     public Date json8() {
  12.         Date date = new Date();
  13.         return date;
  14.     }
复制代码
运行结果:完美无误!



看结果显而易见,
json6处 使用的消息转换器是StringHttpMessageConverter,
因为其最先注册,
而且MappingJackson2HttpMessageConverter仅对响应内容范例为 application/json 的响应体字符串 适配,
此处并不适用;
json7、json8处 使用的消息转换器是MappingJackson2HttpMessageConverter,
本来@ResponseBody方法仅支持寥寥几个范例(即 void、String、ResponseEntity……)作为返回值,
但使用了MappingJackson2HttpMessageConverter之后,可以支持更多的范例作为返回值,
因为MappingJackson2HttpMessageConverter会将返回值都转换成JSON字符串,即String范例,
转换后使用的编码默以为UTF-8
参考详见:
消息转换器 :: Spring 框架 - Spring 中文 (springframework.org.cn)
HTTP消息转换器MappingJackson2HttpMessageConverter生效的几种方式。- 掘金 (juejin.cn)
FastJson

fastjson.jar是阿里开辟的一款专门用于Java开辟的包,可以方便的实现
实现json的转换方法很多,最后的实现结果都是一样的。
pom依靠
  1. <dependency>
  2.     <groupId>com.alibaba</groupId>
  3.     <artifactId>fastjson</artifactId>
  4.     <version>1.2.4</version>
  5. </dependency>
复制代码
三个主要的类

代码测试

新建一个FastJsonDemo 类
  1. package com.moondream;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.moondream.pojo.User;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class FastJsonDemo {
  8.     public static void main(String[] args) {
  9.         //创建一个对象
  10.         User user1 = new User("用户1号", 4, "男");
  11.         User user2 = new User("用户2号", 3, "女");
  12.         User user3 = new User("用户3号", 2, "武装直升机");
  13.         User user4 = new User("用户4号", 1, "沃尔玛购物袋");
  14.         List<User> list = new ArrayList<>();
  15.         list.add(user1);
  16.         list.add(user2);
  17.         list.add(user3);
  18.         list.add(user4);
  19.         System.out.println("*******Java对象 转 JSON字符串*******");
  20.         String str1 = JSON.toJSONString(list);
  21.         System.out.println(str1);
  22.         String str2 = JSON.toJSONString(user1);
  23.         System.out.println(str2);
  24.         System.out.println("\n****** JSON字符串 转 Java对象*******");
  25.         User jp_user1 = JSON.parseObject(str2, User.class);
  26.         System.out.println(jp_user1);
  27.         System.out.println("\n****** Java对象 转 JSON对象 ******");
  28.         JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
  29.         System.out.println(jsonObject1.getString("name"));
  30.         System.out.println("\n****** JSON对象 转 Java对象 ******");
  31.         User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
  32.         System.out.println(to_java_user);
  33.     }
  34. }
复制代码
运行结果:

这种工具类,我们只需要把握使用就好了,在使用的时候在根据详细的业务去找对应的实现。和以前的commons-io那种工具包一样,拿来用就好了!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4