实体类:
1package com.nf.redisDemo1.entity; 2 3 4public class News { 5 6 private long id; 7 private String title; 8 private String body; 9 10 public News() { 11 } 12 13 public News(String title, String body) { 14 this.title = title; 15 this.body = body; 16 } 17 18 public long getId() { 19 return id; 20 } 21 22 public void setId(long id) { 23 this.id = id; 24 } 25 26 public String getTitle() { 27 return title; 28 } 29 30 public void setTitle(String title) { 31 this.title = title; 32 } 33 34 public String getBody() { 35 return body; 36 } 37 38 public void setBody(String body) { 39 this.body = body; 40 } 41 42 @Override 43 public String toString() { 44 return "News{" + 45 "id=" + id + 46 ", title='" + title + '\'' + 47 ", body='" + body + '\'' + 48 '}'; 49 } 50}
Gson实现:
1package com.nf.blog; 2 3import com.google.gson.Gson; 4import com.google.gson.reflect.TypeToken; 5import com.nf.redisDemo1.entity.News; 6 7import java.util.ArrayList; 8import java.util.List; 9 10public class Main { 11 12 public static void main(String[] args) { 13 News news = new News("2019快来了?你准备怎么过年呢?", "身为一个热爱IT行业的程序员,当然是写程序搞事情了。"); 14 Gson gson = new Gson(); 15 16 //单个对象的序列化及反序列化 17 18 //序列化 19 String newsStr = gson.toJson(news); 20 System.out.println("JSON字符串:"); 21 System.out.println(newsStr); 22 23 //反序列化 24 News news_new = gson.fromJson(newsStr, News.class); 25 System.out.println("实体对象:"); 26 System.out.println(news_new); 27 28 //List集合的序列化和反序列化 29 List<News> newsList = new ArrayList<>(); 30 newsList.add(news); 31 newsList.add(new News("你想做的是什么呢?","多写代码,知道闭眼写一个项目为止。")); 32 33 //序列化 34 String newListStr = gson.toJson(newsList); 35 System.out.println("集合序列化后:"); 36 System.out.println(newListStr); 37 38 //反序列化 39 List<News> newsList_new = gson.fromJson(newListStr, new TypeToken<List<News>>() {}.getType()); 40 System.out.println("JSON字符串反序列化:"); 41 System.out.println(newsList_new); 42 43 44 } 45 46}
jascson 方式实现:
1package com.nf.blog; 2 3import com.fasterxml.jackson.core.JsonProcessingException; 4import com.fasterxml.jackson.core.type.TypeReference; 5import com.fasterxml.jackson.databind.ObjectMapper; 6import com.google.gson.Gson; 7import com.google.gson.reflect.TypeToken; 8import com.nf.redisDemo1.entity.News; 9 10import java.io.IOException; 11import java.util.ArrayList; 12import java.util.List; 13 14public class Main { 15 16 public static void main(String[] args) throws IOException { 17 News news = new News("2019快来了?你准备怎么过年呢?", "身为一个热爱IT行业的程序员,当然是写程序搞事情了。"); 18 ObjectMapper objectMapper = new ObjectMapper(); 19 20 // jackson 单个对象的序列化及反序列化 21 //序列化 22 String newsStr = objectMapper.writeValueAsString(news); 23 System.out.println("JSON字符串:"); 24 System.out.println(newsStr); 25 26 //反序列化 27 News news_new = objectMapper.readValue(newsStr,News.class); 28 System.out.println("实体对象:"); 29 System.out.println(news_new); 30 31 //List集合的序列化和反序列化 32 List<News> newsList = new ArrayList<>(); 33 newsList.add(news); 34 newsList.add(new News("你想做的是什么呢?","多写代码,知道闭眼写一个项目为止。")); 35 36 //序列化 37 String newListStr = objectMapper.writeValueAsString(newsList); 38 System.out.println("集合序列化后:"); 39 System.out.println(newListStr); 40 41 //反序列化 42 List<News> newsList_new = objectMapper.readValue(newListStr, new TypeReference<List<News>>(){}); 43 System.out.println("JSON字符串反序列化成List集合:"); 44 System.out.println(newsList_new); 45 46 News[] newsArr = objectMapper.readValue(newListStr,News[].class); 47 System.out.println("JSON字符串反序列化成数组:"); 48 System.out.println(newsArr); 49 50 51 } 52 53}
学无止境(LC)