1 1 package gson; 2 2 3 3 import java.util.ArrayList; 4 4 import java.util.List; 5 5 6 6 import com.google.gson.Gson; 7 7 import com.google.gson.GsonBuilder; 8 8 import com.google.gson.reflect.TypeToken; 9 9 10 10 /** 11 11 * Gson:解析json工具基础用法加高级进阶 12 12 * 13 13 * ClassName: GsonUtils 14 14 * TypeToken:只是为了提取泛型的类型而已 15 15 * 控制缩进:final Gson gson = new GsonBuilder().setVersion(1.1).setPrettyPrinting().create(); 16 16 * 调用setPrettyPrinting方法,改变gson对象的默认行 17 17 * 18 18 * @Description: TODO 19 19 * @author liuhl 20 20 * @date 2018年3月1日 21 21 */ 22 22 public class GsonUtils { 23 23 24 24 public static final Gson gson = new GsonBuilder().create(); 25 25 26 26 /* 基本数据类型的解析 */ 27 27 int number = gson.fromJson("1", int.class); 28 28 double money = gson.fromJson("800.25", double.class); 29 29 boolean bool = gson.fromJson("true", boolean.class); 30 30 String str = gson.fromJson("kele", String.class); 31 31 /* 基本数据类型的解析 */ 32 32 33 33 /* 基本数据类型的生成 */ 34 34 String istr = gson.toJson(1); 35 35 String dstr = gson.toJson(9000.88); 36 36 String bstr = gson.toJson("true"); 37 37 String str2 = gson.toJson("baihe"); 38 38 /* 基本数据类型的生成 */ 39 39 40 40 /* 实体类的解析与生成 */ 41 41 Ticket ticket = new Ticket("2002", 33333.33, "1.1.230010", true); 42 42 String json = gson.toJson(ticket); 43 43 Ticket tick = gson.fromJson(json, Ticket.class); 44 44 /* 实体类的解析与生成 */ 45 45 46 46 /* json转为数组 */ 47 47 String jsonss = "[‘aa‘,‘bb‘,‘cc‘,‘dd‘]"; 48 48 String[] ss = gson.fromJson(jsonss, String[].class); 49 49 String jsonsss = gson.toJson(ss); 50 50 /* json转为数组 */ 51 51 52 52 /* json <----> List */ 53 53 String ljson = "[‘username‘,‘age‘,‘data‘]"; 54 54 List<String> list = gson.fromJson(ljson, new TypeToken<List<String>>() {}.getType()); 55 55 String listjson = gson.toJson(list); 56 56 /* json <----> List */ 57 57 58 58 /* 泛型的使用 */ 59 59 User<Ticket> user = new User<Ticket>("10001", 25, ticket); 60 60 String userjson = gson.toJson(user); 61 61 User<Ticket> userticket = gson.fromJson(userjson, new TypeToken<User<Ticket>>() {}.getType()); 62 62 /* 泛型的使用 */ 63 63 64 64 65 65 66 66 67 67 public static void main(String[] args) { 68 68 /*扩展*/ 69 69 List<Ticket> ticList = new ArrayList<>(); 70 70 ticList.add(new Ticket("3003", 444.444, "2.2.30003", false)); 71 71 ticList.add(new Ticket("4004", 444.444, "4.4.30003", true)); 72 72 ticList.add(new Ticket("5005", 444.444, "5.5.30003", false)); 73 73 ticList.add(new Ticket("6006", 444.444, "6.6.30003", true)); 74 74 ticList.add(new Ticket("7007", 444.444, "7.7.30003", false)); 75 75 76 76 User<List<Ticket>> userlistTic = new User<List<Ticket>>("lhl", 28, ticList); 77 77 String json = gson.toJson(userlistTic); 78 78 User<List<Ticket>> ticuserList = gson.fromJson(json, new TypeToken<User<List<Ticket>>>(){}.getType()); 79 79 /*扩展*/ 80 80 System.out.println(ticuserList.age); 81 81 System.out.println(ticuserList.usercode); 82 82 for (Ticket ticket : ticuserList.data) { 83 83 System.out.println(ticket.dealCode); 84 84 System.out.println(ticket.instnCode); 85 85 System.out.println(ticket.dealFlag); 86 86 System.out.println(ticket.money); 87 87 } 88 88 } 89 89 } 90
原文地址:https://www.cnblogs.com/lhl-shubiao/p/gson.html
时间: 2024-10-14 00:40:22