一、maven安裝jackson依賴
- <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.12.3</version>
- </dependency>
二、Jackson的使用
實(shí)體類轉(zhuǎn)化JSON
把實(shí)體類轉(zhuǎn)化為JSON格式數(shù)據(jù),返回給前端
創(chuàng)建 ObjectMapper obj = new ObjectMapper(); 對(duì)象,對(duì)象的 writeValueAsString 方法 會(huì)把一個(gè)實(shí)體類(必須有g(shù)et、set方法)轉(zhuǎn)化為JSON對(duì)象。
- package com.lxc.springboot.controller;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController // 這個(gè)類下邊的所有方法,都會(huì)返回json,不會(huì)返回一個(gè)視圖!
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- User user = new User("呂星辰", "888", 20);
- ObjectMapper obj = new ObjectMapper();
- String jsonObject = obj.writeValueAsString(user);
- return jsonObject;
- }
- // 為測試方便,在這里寫一個(gè)實(shí)體類
- public static class User {
- private String userName;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- private String password;
- private int age;
- public User(String userName, String password, int age) {
- this.userName = userName;
- this.password = password;
- this.age = age;
- }
- }
- }
測試:
集合轉(zhuǎn)化JSON
前端結(jié)果是:一個(gè)數(shù)組,里邊是一個(gè)個(gè)對(duì)象
- package com.lxc.springboot.controller;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.List;
- @RestController
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- // 創(chuàng)建一個(gè)集合
- List<User> userList = new ArrayList<>();
- for(int i = 0; i < 3; i ++) {
- userList.add(new User("用戶名"+i, "密碼"+i, 20+i));
- }
- ObjectMapper obj = new ObjectMapper();
- String jsonObject = obj.writeValueAsString(userList);
- return jsonObject;
- }
- // 上邊有實(shí)體類,這里省略
- }
測試:
Map轉(zhuǎn)化JSON
- @RestController
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- // 創(chuàng)建一個(gè)Map
- Map<String, Object> map = new HashMap<>();
- map.put("name", "測試名");
- map.put("age", 20);
- ObjectMapper obj = new ObjectMapper();
- String jsonObject = obj.writeValueAsString(map);
- return jsonObject;
- }
- }
前端結(jié)果是:對(duì)象
new Date() 轉(zhuǎn)化JSON
- @RestController
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- Date date = new Date();
- ObjectMapper obj = new ObjectMapper();
- String jsonObject = obj.writeValueAsString(date);
- return jsonObject;
- }
- }
前端結(jié)果是:時(shí)間戳
當(dāng)然,也可以自定義時(shí)間格式
- @RestController
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- String time = sdf.format(date); // "2021-06-27 05:19:33"
- ObjectMapper obj = new ObjectMapper();
- String jsonObject = obj.writeValueAsString(time);
- return jsonObject;
- }
- }
封裝
- package com.lxc.springboot.utils;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializationFeature;
- import java.text.SimpleDateFormat;
- public class JavaUtils {
- /**
- * 使用下邊方法需要導(dǎo)入依賴包:
- * <dependency>
- * <groupId>com.fasterxml.jackson.core</groupId>
- * <artifactId>jackson-databind</artifactId>
- * <version>2.12.3</version>
- * </dependency>
- *
- * @param object 集合(List)、Map(HashMap)、對(duì)象(new Date)
- * @param format 時(shí)間格式化 yyyy-MM-dd hh:mm:ss
- * @return JSON格式化的字符串
- */
- public static String getJson(Object object, String format) {
- ObjectMapper objectMapper = new ObjectMapper();
- // 不使用時(shí)間戳的方式
- objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
- // 自定義時(shí)間格式
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- // 設(shè)置時(shí)間格式化
- objectMapper.setDateFormat(sdf);
- try {
- String jsonValue = objectMapper.writeValueAsString(object);
- return jsonValue;
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String getJson(Object object) {
- return getJson(object, "yyyy-MM-dd hh:mm:ss");
- }
- }
二、FastJson的使用
使用maven導(dǎo)入依賴包
- <!--下邊依賴跟aop沒關(guān)系,只是項(xiàng)目中用到了 JSONObject,所以引入fastjson-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.70</version>
- </dependency>
常用方法:
(1)JSON.toJSONString(obejct) - java對(duì)象轉(zhuǎn)JSON字符串
(2)JSON.parseObject(string, User.class) - JSON字符串轉(zhuǎn)java對(duì)象
使用
- @RestController
- public class Json {
- @RequestMapping(value = "/json")
- public String json() throws Exception{
- List<User> userList = new ArrayList<>();
- userList.add(new User("1", "1", 20));
- String res = JSON.toJSONString(userList);
- return res;
- }
到此這篇關(guān)于Java中常用解析工具jackson及fastjson的使用的文章就介紹到這了,更多相關(guān)jackson和fastjson的使用內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_42778001/article/details/118270837