Gson解析Map格式json資料

2021-07-15 16:31:03 字數 3396 閱讀 7329

有時候我們不需要把實體的所有屬性都匯出,只想把一部分屬性匯出為json.

有時候我們的實體類會隨著版本的公升級而修改.

有時候我們想對輸出的json預設排好格式

這時就可以使用gsonbuilder建立gson,並且配置一些選項。

gson gson = new gsonbuilder()  

.excludefieldswithoutexposeannotation() //不匯出實體中沒有用@expose註解的屬性

.enablecomplexmapkeyserialization() //支援map的key為複雜物件的形式

.serializenulls().setdateformat("yyyy-mm-dd hh:mm:ss:sss")//時間轉化為特定格式

.setfieldnamingpolicy(fieldnamingpolicy.upper_camel_case)//會把字段首字母大寫,注:對於實體上使用了@serializedname註解的不會生效.

.setprettyprinting() //對json結果格式化.

.setversion(1.0) //有的字段不是一開始就有的,會隨著版本的公升級新增進來,那麼在進行序列化和返序列化的時候就會根據版本號來選擇是否要序列化.

//@since(版本號)能完美地實現這個功能.還的字段可能,隨著版本的公升級而刪除,那麼

//@until(版本號)也能實現這個功能,gsonbuilder.setversion(double)方法需要呼叫.

.create();

gson解析各種map格式的json資料

gson gson = new gsonbuilder()  

.enablecomplexmapkeyserialization()

.setprettyprinting()

.create();

hashmapmap =new hashmap();

map.put("1", "數學");

map.put("2", "語文");

map.put("3", "英語");

map.put("4", "物理");

string mapstring =gson.tojson(map);

hashmapmap2 = gson.fromjson(mapstring, new typetoken>(){}.gettype());

log.e("gsonactivity", map2.tostring());

hashmapmap3 =new hashmap();

student student1 =new student(1, "李三", 20);

student student2 =new student(2, "李四", 30);

student student3 =new student(3, "王澤瑞", 40);

map3.put("1",student1);

map3.put("2",student2);

map3.put("3",student3);

string map3string =gson.tojson(map3);

hashmapmap4 = gson.fromjson(map3string, new typetoken>(){}.gettype());

log.e("gsonactivity", map4.tostring());

hashmap> maplist = new hashmap>();

liststudents =new arraylist();

students.add(student1);

students.add(student2);

students.add(student3);

maplist.put("test1", students);

string mapliststring = gson.tojson(maplist);

hashmap> map5 = gson.fromjson(mapliststring, new typetoken>>(){}.gettype());

log.e("gsonactivity", map5.tostring());

輸出結果:

解析date型別的資料格式

gson gson = new gsonbuilder()  

.setprettyprinting()

.setdateformat("yyyy-mm-dd hh:mm:ss")

.create();

student student =new student(1, "李三", 20);

student.birthday = new date();

string jsonstring =gson.tojson(student);

log.e("gsonactivity", jsonstring);

student student2 = gson.fromjson(jsonstring, student.class);

log.e("gsonactivity", student2.tostring() + ", date = " + student2.birthday);

輸出結果:

[id = 1,nickname=李三,age=20], date = wed jul 27 13:38:02 gmt+08:00 2016

解析陣列格式的json資料

gson gson =new gson();

student student1 =new student(1, "李三", 20);

student student2 =new student(2, "李四", 30);

student students =new student;

string jsonstring =gson.tojson(students);

log.e("gsonactivity", jsonstring);

student arraystudents =gson.fromjson(jsonstring, student.class);

for(student student :arraystudents)

輸出結果:

[,]

[id = 1,nickname=李三,age=20]

[id = 2,nickname=李四,age=30]

Gson解析(List和Map)格式json資料

主要解析 兩種格式 列 式 和 map格式 常用的是列表解析,以前不知道解析map,就用json配合gson使用,今天在論壇看到有人問,就試了一下才發現 解析map也很方便,哇喔,又漲姿勢了。public class jsonparse public static void main string ...

json解析之gson解析

我們知道在json中的兩個基本結構是陣列和物件,陣列以括起來,陣列裡的值可以是數字 字串 陣列 物件幾種 物件以 括起來,物件中的資料是以鍵值對的形式出現的,鍵名就是屬性名,鍵值就是屬性值,屬性值的型別可以是 數字 字串 陣列 物件幾種。如果解析的json是乙個物件,那麼我們建乙個這樣的類並以鍵名作...

Gson解析json資料

1 解析單個物件 json字串如下 封裝解析的物件 public class nbamatchs public void setformatdate string formatdate public string getdate public void setdate string date pub...