GSON中Java对象与JSON互相转换——(一)
json的转换插件是通过java的一些工具,直接将java对象或集合转换成json字符串。
常用的json转换工具有如下几种:
1)jsonlib
2)Gson:google
3)fastjson:阿里巴巴
------GSON操作简单,只需要一个jar包----------
User.java
1 package bean; 2 3 public class User { 4 5 private String id; 6 private String name; 7 private int age; 8 private String address; 9 public String getId() { 10 return id; 11 } 12 public void setId(String id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public User(String id, String name, int age, String address) { 22 super(); 23 this.id = id; 24 this.name = name; 25 this.age = age; 26 this.address = address; 27 } 28 @Override 29 public String toString() { 30 return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]"; 31 } 32 33 34 35 }
1.测试Java对象转JSON(测试结果在后面注释)
操作简单,无论转单个对象还是集合,都是gson.toJson(obj);
1 package TestJson; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import javax.xml.bind.UnmarshallerHandler; 9 10 import org.junit.Test; 11 12 import com.google.gson.Gson; 13 14 import bean.User; 15 16 public class Bean2JSON { 17 18 // 测试单个对象转JSON 19 @Test 20 public void test1(){ 21 User user = new User("1", "QIAOZHI",25,"山西太原"); 22 Gson gson = new Gson(); 23 String json = gson.toJson(user); 24 System.out.println(json); //{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"} 25 } 26 27 28 29 // 测试list转JSON 30 @Test 31 public void test2(){ 32 List<User> list = new ArrayList<User>(); 33 list.add(new User("1", "QIAOZHI",25,"山西太原")); 34 list.add(new User("2", "QIAOZHI",25,"山西太原")); 35 list.add(new User("3", "QIAOZHI",25,"山西太原")); 36 Gson gson = new Gson(); 37 String json = gson.toJson(list); 38 System.out.println(json); 39 // [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}] 40 } 41 42 // 测试map对象转JSON 43 @Test 44 public void test3(){ 45 Map<String,User> map = new HashMap<String,User>(); 46 map.put("1", new User("1", "QIAOZHI",25,"山西太原")); 47 map.put("2", new User("2", "QIAOZHI",25,"山西太原")); 48 map.put("3", new User("3", "QIAOZHI",25,"山西太原")); 49 Gson gson = new Gson(); 50 String json = gson.toJson(map); 51 System.out.println(json); 52 // {"3":{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"},"2":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"1":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}} 53 } 54 55 // 测试list<map>对象转JSON 56 @Test 57 public void test4(){ 58 List<Map<String,User>> list = new ArrayList<>(); 59 Map<String,User> map1 = new HashMap<String,User>(); 60 Map<String,User> map2 = new HashMap<String,User>(); 61 map1.put("map11", new User("1", "QIAOZHI",25,"山西太原")); 62 map1.put("map12", new User("2", "QIAOZHI",25,"山西太原")); 63 map2.put("21", new User("11", "QIAOZHI",25,"山西太原")); 64 map2.put("22", new User("21", "QIAOZHI",25,"山西太原")); 65 list.add(map1); 66 list.add(map2); 67 Gson gson = new Gson(); 68 String json = gson.toJson(list); 69 System.out.println(json); 70 // [{"map12":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"map11":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}},{"21":{"id":"11","name":"QIAOZHI","age":25,"address":"山西太原"},"22":{"id":"21","name":"QIAOZHI","age":25,"address":"山西太原"}}] 71 } 72 73 }
2.测试Json转Java对象与集合(测试结果在后面注释)
package TestJson_GSON; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Test; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class JSON2Bean { // 测试json转单个对象 @Test public void test1(){ String json = "{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}"; Gson gson = new Gson(); User user = gson.fromJson(json, User.class); System.out.println(user); // User [id=1, name=QIAOZHI, age=25, address=山西太原] } // 测试JSON转list @SuppressWarnings("unchecked") @Test public void test2(){ String json = "[{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'3','name':'QIAOZHI','age':25,'address':'山西太原'}]"; List<User> list = new ArrayList<User>(); Gson gson = new Gson(); list = (List<User>) gson.fromJson(json, new TypeToken<List<User>>(){}.getType()); System.out.println(list); for(User user:list){ System.out.println(user); } // [{id=1, name=QIAOZHI, age=25.0, address=山西太原}, {id=2, name=QIAOZHI, age=25.0, address=山西太原}, {id=3, name=QIAOZHI, age=25.0, address=山西太原}] System.out.println(list.get(1)); // {id=2, name=QIAOZHI, age=25.0, address=山西太原} } // 测试JSON转Map @Test public void test3(){ String json = "{'3':{'id':'3','name':'QIAOZHI','age':25,'address':'山西太原'},'2':{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},'1':{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}}"; Map<String,User> map = new HashMap<String,User>(); Gson gson = new Gson(); map = (Map<String,User>) gson.fromJson(json, new TypeToken<Map<String,User>>(){}.getType()); System.out.println(map); // {3={id=3, name=QIAOZHI, age=25.0, address=山西太原}, 2={id=2, name=QIAOZHI, age=25.0, address=山西太原}, 1={id=1, name=QIAOZHI, age=25.0, address=山西太原}} User user = map.get("1"); System.out.println(user); // {id=1, name=QIAOZHI, age=25.0, address=山西太原} } // 测试JSON转list<map>对象 @SuppressWarnings("unchecked") @Test public void test4(){ String json = "[{'map12':{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},'map11':{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}},{'21':{'id':'11','name':'QIAOZHI','age':25,'address':'山西太原'},'22':{'id':'21','name':'QIAOZHI','age':25,'address':'山西太原'}}]"; List<Map<String,User>> list = new ArrayList<>(); Gson gson = new Gson(); list = gson.fromJson(json, new TypeToken<List<Map<String,User>>>(){}.getType()); System.out.println(list); // [{map12={id=2, name=QIAOZHI, age=25.0, address=山西太原}, map11={id=1, name=QIAOZHI, age=25.0, address=山西太原}}, {21={id=11, name=QIAOZHI, age=25.0, address=山西太原}, 22={id=21, name=QIAOZHI, age=25.0, address=山西太原}}] System.out.println(list.get(1)); // {21={id=11, name=QIAOZHI, age=25.0, address=山西太原}, 22={id=21, name=QIAOZHI, age=25.0, address=山西太原}} } }
注意:
json转普通的Javabean只需要两个简单的参数,第一个是json字符串,第二个是需要转换对象的类型。
User user = gson.fromJson(json, User.class);
json转集合与map类型需要使用了TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换。也就是通知GSON将json转为那种集合
list = (List<User>) gson.fromJson(json, new TypeToken<List<User>>(){}.getType());
map = (Map<String,User>) gson.fromJson(json, new TypeToken<Map<String,User>>(){}.getType());
list = gson.fromJson(json, new TypeToken<List<Map<String,User>>>(){}.getType());
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】