1. 程式人生 > >將 物件序列化與反序化 (json格式)

將 物件序列化與反序化 (json格式)

實體類

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

 

jackson

 1 package com.nf.blog;
 2 
 3 import com.fasterxml.jackson.core.JsonProcessingException;
 4 import com.fasterxml.jackson.core.type.TypeReference;
 5 import com.fasterxml.jackson.databind.ObjectMapper;
 6 import com.google.gson.Gson;
 7 import com.google.gson.reflect.TypeToken;
 8 import com.nf.redisDemo1.entity.News;
 9 
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13 
14 public 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 }