亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中常用解析工具jackson及fastjson的使用

 更新時(shí)間:2021年06月28日 11:28:45   作者:紅旗下的小兵  
今天給大家?guī)淼氖顷P(guān)于Java解析工具的相關(guān)知識(shí),文章圍繞著jackson及fastjson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下

一、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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論