最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

@Autowired
ObjectMapper objectMapper;

實體轉(zhuǎn)json

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

json轉(zhuǎn)實體

    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ù),各場景實例

該篇內(nèi)容,結(jié)合實例介紹使用jackson來操作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>

示例中使用到的實體類, UserEntity.java

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

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

1. 對象(示例為 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);
    }

控制臺輸出:

格式很漂亮,是因為使用了 :

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

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

readValue 方法

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

控制臺輸出:

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);

控制臺輸出:

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

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

控制臺輸出:

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);

控制臺輸出:

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());

控制臺輸出:

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

現(xiàn)在模擬了一串稍微復雜一些的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
}

那么我們接口接收時,如果操作呢?

1.直接使用 @RequestBody Map map 接收,里面如果嵌套list,那就拿出對應(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把這個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: 為啥我要科普這個jackson的使用么?這個算是基本的操作了,原本我經(jīng)手的很多項目都用到的fastjson ,其實使用起來也杠杠的。

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

懂的人自然會明白。

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

相關(guān)文章

  • 基于Hibernate中配置文件的學習(分享)

    基于Hibernate中配置文件的學習(分享)

    下面小編就為大家?guī)硪黄贖ibernate中配置文件的學習(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SpringBoot使用Kafka來優(yōu)化接口請求的并發(fā)方式

    SpringBoot使用Kafka來優(yōu)化接口請求的并發(fā)方式

    這篇文章主要介紹了SpringBoot使用Kafka來優(yōu)化接口請求的并發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 關(guān)于MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù))

    關(guān)于MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù))

    這篇文章主要介紹了MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù)),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Jenkins+Git+Maven自動化部署配置詳解

    Jenkins+Git+Maven自動化部署配置詳解

    本文主要介紹了Jenkins+Git+Maven自動化部署配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • SpringSecurity解決POST方式下CSRF問題

    SpringSecurity解決POST方式下CSRF問題

    本文主要介紹了SpringSecurity解決POST方式下CSRF問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • SpringCloud中的openFeign調(diào)用服務(wù)并傳參的過程

    SpringCloud中的openFeign調(diào)用服務(wù)并傳參的過程

    服務(wù)和服務(wù)之間通信,不僅僅是調(diào)用,往往在調(diào)用過程中還伴隨著參數(shù)傳遞,接下來重點來看看OpenFeign在調(diào)用服務(wù)時如何傳遞參數(shù),感興趣的朋友一起看看吧
    2023-11-11
  • Spring使用aop切面編程時要給那些類加注解的實例

    Spring使用aop切面編程時要給那些類加注解的實例

    在使用切面編程時,通常需要為以下類或組件添加注解來標識它們,以便 Spring 或其他切面框架能夠正確識別和處理它們,這篇文章主要介紹了Spring使用aop切面編程時要給那些類加注解,需要的朋友可以參考下
    2023-11-11
  • 解析SpringBoot整合SpringDataRedis的過程

    解析SpringBoot整合SpringDataRedis的過程

    這篇文章主要介紹了SpringBoot整合SpringDataRedis的過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • IDEA創(chuàng)建springboot + mybatis項目全過程(步驟詳解)

    IDEA創(chuàng)建springboot + mybatis項目全過程(步驟詳解)

    這篇文章主要介紹了IDEA創(chuàng)建springboot + mybatis項目全過程及步驟詳解,本文通圖文實例代碼相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 一文帶你了解Java萬物之基之Object類

    一文帶你了解Java萬物之基之Object類

    Java是一門天然的面向?qū)ο蟮恼Z言。而所有我們手動創(chuàng)造出來的類,都繼承于同一個類,即Object類。本文將通過示例為大家詳細介紹一下Java中的Object類,需要的可以參考一下
    2022-03-03

最新評論

高青县| 安远县| 邻水| 休宁县| 始兴县| 奉贤区| 灵璧县| 都匀市| 大城县| 乌海市| 四子王旗| 江西省| 西昌市| 合江县| 曲麻莱县| 黄骅市| 彩票| 鹤庆县| 松滋市| 新巴尔虎右旗| 横峰县| 临朐县| 资源县| 扎兰屯市| 乌拉特中旗| 衡阳市| 万全县| 罗江县| 河北省| 重庆市| 卢湾区| 观塘区| 三门峡市| 镇宁| 汶上县| 绥阳县| 丰镇市| 沙坪坝区| 额尔古纳市| 富民县| 太原市|