溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

maven安裝jackson依賴的示例分析

發(fā)布時(shí)間:2021-06-28 14:27:26 來(lái)源:億速云 閱讀:6450 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)maven安裝jackson依賴的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

一、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;
    }
    // 為測(cè)試方便,在這里寫(xiě)一個(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;
        }
    }
}

測(cè)試:

maven安裝jackson依賴的示例分析

集合轉(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í)體類,這里省略
}

 測(cè)試:

maven安裝jackson依賴的示例分析

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", "測(cè)試名");
        map.put("age", 20);
        ObjectMapper obj = new ObjectMapper();
        String jsonObject = obj.writeValueAsString(map);
        return jsonObject;
    }
}

前端結(jié)果是:對(duì)象

maven安裝jackson依賴的示例分析

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í)間戳

maven安裝jackson依賴的示例分析

當(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沒(méi)關(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;
    }

maven安裝jackson依賴的示例分析

感謝各位的閱讀!關(guān)于“maven安裝jackson依賴的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI