Gson方式实现json字符串和JavaBean对象的转换

it2024-12-02  2

客户端与服务器进行数据交互时,常常需要将数据在服务器端将数据转化成字符串并在客户端对json数据进行解析生成对象。但是用jsonObject和jsonArray解析相对麻烦:见博主博客:https://crycrycry.blog.csdn.net/article/details/109108341。利用谷歌的Gson和阿里的fastjson可以很方便的将下面四种常用的数据转换成json字符串,并能够将其解析成原数据对象。

类型一:JavaBean

类型二:List<JavaBean>

类型三:List<String>

类型四:List<Map<String,Object>>

一、Gson将上面的四种数据对象转换成json字符串的方法都是一样的

Gson gson = new Gson(); String jsonString = gson.toJson(obj);

类型1:Gson实现json字符串和JavaBean对象的相互转换

public class Demo { public static void main(String[] args) { Person person = new Person("浙江","张三",1001); Gson gson = new Gson(); //这里将javabean转化成json字符串 String jsonString = gson.toJson(person); System.out.println(jsonString); //这里将json字符串转化成javabean对象 Person person1 = gson.fromJson(jsonString,Person.class); System.out.println(person1.getName()); } } class Person{ private String region; private String name; private int id; public Person(String region, String name, int id) { this.region = region; this.name = name; this.id = id; } //getset方法 public String getName() { return name; } }

打印结果: {“region”:“浙江”,“name”:“张三”,“id”:1001} 张三

类型2:Gson实现json字符串和List对象的相互转换

public class Demo { public static void main(String[] args) { Person person1 = new Person("浙江","张三",1001); Person person2 = new Person("江苏","李四",1002); List<Person> persons = new ArrayList<Person>(); persons.add(person1); persons.add(person2); Gson gson = new Gson(); //这里将list<javabean>转化成json字符串 String jsonString = gson.toJson(persons); System.out.println(jsonString); //将json字符串转化成List<JavaBean>对象 List<Person> persons2 = gson.fromJson(jsonString, new TypeToken<List<Person>>(){}.getType()); for (Person p:persons2) { System.out.println(p.getName()); } } } class Person{ private String region; private String name; private int id; public Person(String region, String name, int id) { this.region = region; this.name = name; this.id = id; } //getset方法 public String getName() { return name; } }

打印结果: [{“region”:“浙江”,“name”:“张三”,“id”:1001},{“region”:“江苏”,“name”:“李四”,“id”:1002}] 张三 李四

类型3:Gson实现json字符串和List对象的相互转换

public class Demo { public static void main(String[] args) { //将List<String>对象转化成json字符串 List<String> list = new ArrayList<String>(); list.add("gson1"); list.add("gson2"); list.add("gson3"); Gson gson = new Gson(); String jsonString = gson.toJson(list); System.out.println("json字符串:" + jsonString); //将json字符串转化成List<String>对象 List<String> list2 = gson.fromJson(jsonString, new TypeToken<List<String>>(){}.getType()); for (String s:list2) { System.out.println(s); } } }

打印结果: json字符串:[“gson1”,“gson2”,“gson3”] gson1 gson2 gson3

类型4:Gson实现json字符串和List<Map<String,Object>>对象的相互转换

public class Demo { public static void main(String[] args) { //将List<Map<String,Object>>对象转化成json字符串 Map<String,Object> map = new HashMap<String,Object>(); map.put("key1", "value1"); map.put("key2", "value2"); Map<String,Object> map2 = new HashMap<String,Object>(); map2.put("key1", 1); map2.put("key2", 2); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); list.add(map); list.add(map2); Gson gson = new Gson(); String jsonString = gson.toJson(list); System.out.println("json字符串:" + jsonString); //将json字符串转化成List<Map<String,Object>>对象 List<Map<String,Object>> list2 = gson.fromJson(jsonString, new TypeToken<List<Map<String,Object>>>(){}.getType()); for (Map<String,Object> m:list2) { System.out.println(m); } } }

打印结果: json字符串:[{“key1”:“value1”,“key2”:“value2”},{“key1”:1,“key2”:2}] {key1=value1, key2=value2} {key1=1.0, key2=2.0}

最新回复(0)