5.@RestController 注解
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j1") public String test() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); // 创建一个对象 User user = new User("吉士先生",21,"男"); String s = mapper.writeValueAsString(user); return s; } }
6.Java对象转JSON字符串【对象】
ObjectMapper mapper = new ObjectMapper(); mapper.writeValueAsString(user);
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j1") @ResponseBody public String test() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); // 创建一个对象 User user = new User("吉士先生",21,"男"); String s = mapper.writeValueAsString(user); return s; } }
7.Java集合转JSON字符串【集合】
ObjectMapper mapper = new ObjectMapper(); mapper.writeValueAsString(userList);
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j2") public String test2() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); List<User> userList = new ArrayList<>(); // 创建一个对象 User user = new User("吉士先生",21,"男"); User user2 = new User("吉士先生2",21,"男"); User user3 = new User("吉士先生3",21,"男"); User user4 = new User("吉士先生4",21,"男"); User user5 = new User("吉士先生5",21,"男"); userList.add(user); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); String s = mapper.writeValueAsString(userList); return s; } }
8.Java日期转JSON字符串【日期】
(1).输出的是时间戳
Date date = new Date(); 输出的话会是一个 时间戳 date.toString(); 输出的话会是一个 GMT
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j3") public String test3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); // 创建一个对象 Date date = new Date(); // 自定义日期格式 String s = mapper.writeValueAsString(date.toString()); return s; } }
(2).时间戳转换为自定义日期格式
把时间戳转换为自定义的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.format(date);
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j3") public String test3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); // 创建一个对象 Date date = new Date(); // 自定义日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = mapper.writeValueAsString(sdf.format(date)); return s; } }
(3).自定义日期格式+更改配置
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j3") public String test3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); // 创建一个对象 Date date = new Date(); // 自定义日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(sdf); String s = mapper.writeValueAsString(date); return s; } }
(十四)、FastJson
1.什么是FastJson?
在前后端数据传输交互中,经常会遇到字符串(String)与json,XML等格式相互转换与解析,其中json以跨语言,跨前后端的优点在开发中被频繁使用,基本上可以说是标准的数据交换格式。fastjson 是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致.它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中。可以方便的实现Json对象与JavaBean对象的转换,实现JavaBEAN对象与json字符串的转换。
2.搭配环境
(1).导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
(2).在Aftrical里面的lib目录添加jar包
3.FastJson实战演练
System.out.println("********Java 对象 转 JSON 字符串*********"); String s = JSON.toJSONString(userList); System.out.println("Java 对象转换Json字符串为:"+userList); System.out.println("\n********JSON 字符串 转 Java 对象*********"); User user1 = JSON.parseObject(JSON.toJSONString(user2),User.class); System.out.println("JSON 字符串转换Java对象为:"+user1); System.out.println("\n********Java 对象 转 JSON 对象*********"); JSONObject o = (JSONObject) JSON.toJSON(user2); System.out.println("Java 对象转换为JSON对象后为:"+o.getString("name")); System.out.println("\n********JSON 对象 转 Java 对象*********"); User user6 = JSON.toJavaObject(o, User.class); System.out.println("JSON 对象转换Java 对象后为:"+user6);
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController //这个注解直接不走视图解析器了,实现前后端分离 public class UserController { @RequestMapping("/j4") public String test4(){ List<User> userList = new ArrayList<>(); User user = new User("吉士先生",21,"男"); User user2 = new User("吉士先生2",21,"男"); User user3 = new User("吉士先生3",21,"男"); User user4 = new User("吉士先生4",21,"男"); User user5 = new User("吉士先生5",21,"男"); userList.add(user); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); System.out.println("********Java 对象 转 JSON 字符串*********"); String s = JSON.toJSONString(userList); System.out.println("Java 对象转换Json字符串为:"+userList); System.out.println("\n********JSON 字符串 转 Java 对象*********"); User user1 = JSON.parseObject(JSON.toJSONString(user2),User.class); System.out.println("JSON 字符串转换Java对象为:"+user1); System.out.println("\n********Java 对象 转 JSON 对象*********"); JSONObject o = (JSONObject) JSON.toJSON(user2); System.out.println("Java 对象转换为JSON对象后为:"+o.getString("name")); System.out.println("\n********JSON 对象 转 Java 对象*********"); User user6 = JSON.toJavaObject(o, User.class); System.out.println("JSON 对象转换Java 对象后为:"+user6); return s; } }