gson對象序列化的示例
1.編寫核心類
MainApp:
package com.yiidian.gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.*; public class MainApp { public static void main(String args[]) { MainApp tester = new MainApp(); try { Student student = new Student(); student.setAge(10); student.setName("eric"); tester.writeJSON(student); Student student1 = tester.readJSON(); System.out.println(student1); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } //把Java對象存儲student.json文件 private void writeJSON(Student student) throws IOException { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); FileWriter writer = new FileWriter("student.json"); writer.write(gson.toJson(student)); writer.close(); } //從student.json文件讀取Java對象 private Student readJSON() throws FileNotFoundException { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); BufferedReader bufferedReader = new BufferedReader( new FileReader("student.json")); Student student = gson.fromJson(bufferedReader, Student.class); return student; } } class Student { private String name; private int age; public Student(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "Student [ name: "+name+", age: "+ age+ " ]"; } }
2 運(yùn)行測試
控制臺輸出:
項(xiàng)目下生成student.json文件
以上就是gson對象序列化的示例的詳細(xì)內(nèi)容,更多關(guān)于Gson-對象序列化的資料請關(guān)注腳本之家其它相關(guān)文章!
- kotlin gson反序列化默認(rèn)值失效深入講解
- java如何利用FastJSON、Gson、Jackson三種Json格式工具自定義時(shí)間序列化
- GSON實(shí)現(xiàn)Java對象的JSON序列化與反序列化的實(shí)例教程
- Java中Gson的使用詳解
- Java實(shí)現(xiàn)操作JSON的便捷工具類完整實(shí)例【重寫Google的Gson】
- Java中利用gson解析Json實(shí)例教程
- Gson解析空字符串發(fā)生異常的處理方法
- Android利用Gson解析嵌套多層的Json的簡單方法
- GSON實(shí)現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程
- 舉例講解Java的JSON類庫GSON的基本用法
- Java的JSON格式轉(zhuǎn)換庫GSON的初步使用筆記
- Gson如何序列化內(nèi)部類
相關(guān)文章
Java_int、double型數(shù)組常用操作工具類(分享)
下面小編就為大家?guī)硪黄狫ava_int、double型數(shù)組常用操作工具類(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Java中FilterInputStream和FilterOutputStream的用法詳解
這篇文章主要介紹了Java中FilterInputStream和FilterOutputStream的用法詳解,這兩個(gè)類分別用于封裝輸入和輸出流,需要的朋友可以參考下2016-06-06JSON--List集合轉(zhuǎn)換成JSON對象詳解
這篇文章主要介紹了List集合轉(zhuǎn)換成JSON對象,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2017-01-01Spring Boot 中application.yml與bootstrap.yml的區(qū)別
其實(shí)yml和properties文件是一樣的原理,且一個(gè)項(xiàng)目上要么yml或者properties,二選一的存在。這篇文章給大家介紹了Spring Boot 中application.yml與bootstrap.yml的區(qū)別,感興趣的朋友一起看看吧2018-04-04Java關(guān)鍵字、標(biāo)識符、常量、變量語法詳解
這篇文章主要為大家詳細(xì)介紹了Java關(guān)鍵字、標(biāo)識符、常量、變量等基礎(chǔ)語法,感興趣的小伙伴們可以參考一下2016-09-09Java fastdfs客戶端實(shí)現(xiàn)上傳下載文件
這篇文章主要介紹了Java fastdfs客戶端實(shí)現(xiàn)上傳下載文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法
短信驗(yàn)證功能在各個(gè)網(wǎng)站應(yīng)用都非常廣泛,那么在springmvc中如何實(shí)現(xiàn)短信驗(yàn)證碼功能呢?今天小編抽時(shí)間給大家介紹下Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,一起看看吧2016-09-09