欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jackson 如何將實(shí)體轉(zhuǎn)json json字符串轉(zhuǎn)實(shí)體

 更新時(shí)間:2021年10月19日 10:20:59   作者:介寒食  
這篇文章主要介紹了jackson 實(shí)現(xiàn)將實(shí)體轉(zhuǎn)json json字符串轉(zhuǎn)實(shí)體,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將實(shí)體轉(zhuǎn)json json字符串轉(zhuǎn)實(shí)體

@Autowired
ObjectMapper objectMapper;

實(shí)體轉(zhuǎn)json

  String data = "";  //一個(gè)json串
  Student stu = new Student ();
 stu = objectMapper.readValue(data, Student .class);// json字符串轉(zhuǎn)實(shí)體
public <T> String writeAsString(T t) throws JsonProcessingException {
    return objectMapper.writeValueAsString(t);
} 
String aa = writeAsString(stu);

json轉(zhuǎn)實(shí)體

    public <T> T readValue(String data) {
        try {
            return objectMapper.readValue(data, new TypeReference<T>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

使用Jackson操作json數(shù)據(jù),各場(chǎng)景實(shí)例

該篇內(nèi)容,結(jié)合實(shí)例介紹使用jackson來(lái)操作json數(shù)據(jù):

在pom.xml文件中添加 ,Jackson 依賴:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.1</version>
        </dependency>
 
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.1</version>
        </dependency>

示例中使用到的實(shí)體類, UserEntity.java

/**
 * @Author : JCccc
 * @CreateTime : 2020/3/18
 * @Description :
 **/
public class UserEntity {
    private Integer id;
    private String name;
    private Integer age;
 
  // set get 方法 和 toString 等方法就不粘貼出來(lái)了
}

在介紹示例前,先看一張圖,使用jackson如:

1. 對(duì)象(示例為 UserEntity)轉(zhuǎn) json 數(shù)據(jù)

writeValueAsString 方法

    public static void main(String[] args) throws JsonProcessingException {
        UserEntity userEntity=new UserEntity();
        userEntity.setId(100);
        userEntity.setName("JCccc");
        userEntity.setAge(18);
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(userEntity);
        System.out.println(jsonString);
    }

控制臺(tái)輸出:

格式很漂亮,是因?yàn)槭褂昧?:

咱們不需要漂亮,所以后面的我都不使用格式的方法了,轉(zhuǎn)換的時(shí)候,只需要 writeValueAsString 就夠了 。

2. json 數(shù)據(jù) 轉(zhuǎn) 對(duì)象

readValue 方法

        ObjectMapper mapper = new ObjectMapper();
        //json字符串轉(zhuǎn)對(duì)象
        UserEntity userEntityNew = mapper.readValue(jsonString, UserEntity.class);
        System.out.println(userEntityNew);

控制臺(tái)輸出:

3. map 轉(zhuǎn) json 數(shù)據(jù)

        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("A",1);
        map.put("B",2);
        map.put("C",3);
        map.put("D",4);
        String jsonMap = mapper.writeValueAsString(map);
        System.out.println(jsonMap);

控制臺(tái)輸出:

4. json 數(shù)據(jù) 轉(zhuǎn) map

        ObjectMapper mapper = new ObjectMapper();
        //json字符串轉(zhuǎn)為Map對(duì)象
        Map mapNew=mapper.readValue(jsonMap, Map.class);
        System.out.println(mapNew);

控制臺(tái)輸出:

5. List<UserEntity> 轉(zhuǎn) json 數(shù)據(jù)

        ObjectMapper mapper = new ObjectMapper();
        UserEntity userEntity1=new UserEntity();
        userEntity1.setId(101);
        userEntity1.setName("JCccc1");
        userEntity1.setAge(18);
        UserEntity userEntity2=new UserEntity();
        userEntity2.setId(102);
        userEntity2.setName("JCccc2");
        userEntity2.setAge(18);
        UserEntity userEntity3=new UserEntity();
        userEntity3.setId(103);
        userEntity3.setName("JCccc3");
        userEntity3.setAge(18);
        List<UserEntity> userList=new ArrayList<>();
        userList.add(userEntity1);
        userList.add(userEntity2);
        userList.add(userEntity3);
        
        String jsonList = mapper.writeValueAsString(userList);
        System.out.println(jsonList);

控制臺(tái)輸出:

6. json 數(shù)據(jù) 轉(zhuǎn) List<UserEntity>

        ObjectMapper mapper = new ObjectMapper();
        List<UserEntity> userListNew= mapper.readValue(jsonList,new TypeReference<List<UserEntity>>(){});
        System.out.println(userListNew.toString());

控制臺(tái)輸出:

7.接口接收稍微復(fù)雜一點(diǎn)的json數(shù)據(jù),如何拆解

現(xiàn)在模擬了一串稍微復(fù)雜一些的json數(shù)據(jù),如:

{
	"msg": "success",
	"data": [
		{
			"id": 101,
			"name": "JCccc1",
			"age": 18
		},
		{
			"id": 102,
			"name": "JCccc2",
			"age": 18
		},
		{
			"id": 103,
			"name": "JCccc3",
			"age": 18
		}
	],
	"status": 200
}

那么我們接口接收時(shí),如果操作呢?

1.直接使用 @RequestBody Map map 接收,里面如果嵌套list,那就拿出對(duì)應(yīng)的value轉(zhuǎn)list,然后該怎么拿怎么拿。

    @PostMapping("testJackson")
    public void testJackson(@RequestBody Map map) {
 
        System.out.println(map);
        String  msg = String.valueOf(map.get("msg"));
        System.out.println(msg);
        List dataList = (List) map.get("data");
        System.out.println(dataList.toString());
    }

2.使用字符串接收json數(shù)據(jù) @RequestBody String jsonStr , 那么就使用jackson把這個(gè)json數(shù)據(jù)轉(zhuǎn)為Map,然后該怎么拿怎么拿。

    @PostMapping("testJackson")
    public void testJackson(@RequestBody String jsonStr) throws JsonProcessingException {
 
        System.out.println(jsonStr);
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.readValue(jsonStr, Map.class);
        String  msg = String.valueOf(map.get("msg"));
        System.out.println(msg);
        String  status = String.valueOf(map.get("status"));
        System.out.println(status);
        List dataList = (List) map.get("data");
        System.out.println(dataList.toString());
    }

好的,該篇就到此。

ps: 為啥我要科普這個(gè)jackson的使用么?這個(gè)算是基本的操作了,原本我經(jīng)手的很多項(xiàng)目都用到的fastjson ,其實(shí)使用起來(lái)也杠杠的。

除了jackson是springboot web包的內(nèi)部解析框架外,其實(shí)還有一些原因。

懂的人自然會(huì)明白。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合logback的示例代碼

    SpringBoot整合logback的示例代碼

    Logback是由log4j創(chuàng)始人設(shè)計(jì)的又一個(gè)開(kāi)源日志組件,logback分為三個(gè)模塊,在文章開(kāi)頭給大家介紹的很明確,接下來(lái)通過(guò)本文重點(diǎn)介紹下SpringBoot整合logback的方法,需要的朋友可以參考下
    2022-04-04
  • Spring Boot項(xiàng)目中集成微信支付v3

    Spring Boot項(xiàng)目中集成微信支付v3

    這篇文章主要介紹了Spring Boot項(xiàng)目中集成微信支付v3,幫助大家更好的理解和使用spring boot框架,感興趣的朋友可以了解下
    2021-01-01
  • java泛型基本知識(shí)和通用方法

    java泛型基本知識(shí)和通用方法

    這篇文章主要介紹了java泛型基礎(chǔ)知識(shí)及通用方法,從以下幾個(gè)方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類和接口,感興趣的可以了解一下
    2021-06-06
  • java版簡(jiǎn)單的猜數(shù)字游戲?qū)嵗a

    java版簡(jiǎn)單的猜數(shù)字游戲?qū)嵗a

    猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說(shuō)簡(jiǎn)單也很簡(jiǎn)單,說(shuō)不簡(jiǎn)單確實(shí)也很難,那么下面這篇文章主要給大家介紹了java版簡(jiǎn)單的猜數(shù)字游戲的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)分析和示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)

    SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)

    這篇文章主要介紹了SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • maven項(xiàng)目打包上傳到私有倉(cāng)庫(kù)

    maven項(xiàng)目打包上傳到私有倉(cāng)庫(kù)

    在項(xiàng)目開(kāi)發(fā)中通常會(huì)引用其他的jar,怎樣把自己的項(xiàng)目做為一個(gè)jar包的形式發(fā)布到私服倉(cāng)庫(kù)中,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-06-06
  • 從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析)

    從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析)

    下面小編就為大家分享一篇從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 詳解Java注解的實(shí)現(xiàn)與使用方法

    詳解Java注解的實(shí)現(xiàn)與使用方法

    這篇文章主要介紹了詳解Java注解的實(shí)現(xiàn)與使用方法的相關(guān)資料,希望通過(guò)本文大家能夠理解掌握J(rèn)ava注解的知識(shí),需要的朋友可以參考下
    2017-09-09
  • java和matlab畫(huà)多邊形閉合折線圖示例講解

    java和matlab畫(huà)多邊形閉合折線圖示例講解

    由于要將“哈密頓回路問(wèn)題(TSP)”的求解中間結(jié)果表示出來(lái),查了一下使用程序畫(huà)多邊形圖形?,F(xiàn)在在總結(jié)一下,這個(gè)圖是“由給定節(jié)點(diǎn)首尾相連的”閉合多邊形
    2014-02-02

最新評(píng)論