Gson、jackson 序列化,反序列化(单个、集合)

实体类:

复制代码
package com.nf.redisDemo1.entity;


public class News {

    private long id;
    private String title;
    private String body;

    public News() {
    }

    public News(String title, String body) {
        this.title = title;
        this.body = body;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", body='" + body + '\'' +
                '}';
    }
}
复制代码

Gson实现:

复制代码
package com.nf.blog;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.redisDemo1.entity.News;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        News news = new News("2019快来了?你准备怎么过年呢?", "身为一个热爱IT行业的程序员,当然是写程序搞事情了。");
        Gson gson = new Gson();

        //单个对象的序列化及反序列化

        //序列化
        String newsStr = gson.toJson(news);
        System.out.println("JSON字符串:");
        System.out.println(newsStr);

        //反序列化
        News news_new = gson.fromJson(newsStr, News.class);
        System.out.println("实体对象:");
        System.out.println(news_new);

        //List集合的序列化和反序列化
        List<News> newsList = new ArrayList<>();
        newsList.add(news);
        newsList.add(new News("你想做的是什么呢?","多写代码,知道闭眼写一个项目为止。"));

        //序列化
        String newListStr = gson.toJson(newsList);
        System.out.println("集合序列化后:");
        System.out.println(newListStr);

        //反序列化
        List<News> newsList_new = gson.fromJson(newListStr, new TypeToken<List<News>>() {}.getType());
        System.out.println("JSON字符串反序列化:");
        System.out.println(newsList_new);


    }

}
复制代码

 

 jascson 方式实现:

复制代码
package com.nf.blog;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.redisDemo1.entity.News;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        News news = new News("2019快来了?你准备怎么过年呢?", "身为一个热爱IT行业的程序员,当然是写程序搞事情了。");
        ObjectMapper objectMapper = new ObjectMapper();

        // jackson 单个对象的序列化及反序列化
        //序列化
        String newsStr = objectMapper.writeValueAsString(news);
        System.out.println("JSON字符串:");
        System.out.println(newsStr);

        //反序列化
        News news_new = objectMapper.readValue(newsStr,News.class);
        System.out.println("实体对象:");
        System.out.println(news_new);

        //List集合的序列化和反序列化
        List<News> newsList = new ArrayList<>();
        newsList.add(news);
        newsList.add(new News("你想做的是什么呢?","多写代码,知道闭眼写一个项目为止。"));

        //序列化
        String newListStr = objectMapper.writeValueAsString(newsList);
        System.out.println("集合序列化后:");
        System.out.println(newListStr);

        //反序列化
        List<News> newsList_new = objectMapper.readValue(newListStr, new TypeReference<List<News>>(){});
        System.out.println("JSON字符串反序列化成List集合:");
        System.out.println(newsList_new);

        News[] newsArr = objectMapper.readValue(newListStr,News[].class);
        System.out.println("JSON字符串反序列化成数组:");
        System.out.println(newsArr);


    }

}
复制代码

 

 

止境(LC)

 

posted @   可可西里(lemon)  阅读(2250)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示