Gson入门教程【原】
gson一个jar包就能纵横天下,不像Json-lib.jar依赖其它jar包.
http://central.maven.org/maven2/com/google/code/gson/gson/2.1/gson-2.1.jar
定义一个JavaBean类Person:
package ccc.json; public class Person { Integer id; String name; String place; public Person(Integer id, String name, String place) { super(); this.id = id; this.name = name; this.place = place; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", place=" + place + "]"; } }
再定义一个JsonService.
用于生成各种形式的对象如Object,List,List<Map<String,Object>> :
package ccc.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class JsonService { public Person getPerson(){ Person person=new Person(123,"bobo","HangZhou"); return person; } public List<Person> getPersonList(){ List<Person> list=new ArrayList<Person>(); Person person1 = new Person(123,"sisi","BeiJing"); Person person2 = new Person(2002, "bobo", "HangZhou"); list.add(person1); list.add(person2); return list; } public List<String> getList(){ List<String> list=new ArrayList<String>(); list.add("BeiJing"); list.add("HangZhou"); return list; } public Map<String,Object> getMap(){ Map<String,Object> map=new HashMap<String, Object>(); map.put("id", 123); map.put("name","sisi"); map.put("place", "BeiJing"); return map; } public List<Map<String,Object>> getlistMap(){ List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); Map<String,Object> map1=new HashMap<String, Object>(); map1.put("id", 123); map1.put("name","sisi"); map1.put("place", "BeiJing"); Map<String,Object> map2=new HashMap<String, Object>(); map2.put("id", 456); map2.put("name","bobo"); map2.put("place", "HangZhou"); list.add(map1); list.add(map2); return list; } }
GsonDemo入口:
package ccc.json; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class Json_demo { public static void main(String[] args) { Gson gson = new Gson(); JsonService service = new JsonService(); String str1 = gson.toJson(service.getPerson()); System.out.println("1: "+str1); // 1: {"id":123,"name":"bobo","place":"HangZhou"} String str4 = gson.toJson(service.getMap()); System.out.println("2: "+str4); // 2: {"id":123,"name":"sisi","place":"BeiJing"} String str2 = gson.toJson(service.getPersonList()); System.out.println("3: "+str2); // 3: [{"id":123,"name":"sisi","place":"BeiJing"},{"id":2002,"name":"bobo","place":"HangZhou"}] String str3 = gson.toJson(service.getList()); System.out.println("4: "+str3); // 4: ["BeiJing","HangZhou"] String str5 = gson.toJson(service.getlistMap()); System.out.println("5: "+str5); // 5: [{"id":123,"name":"sisi","place":"BeiJing"},{"id":456,"name":"bobo","place":"HangZhou"}] Person personA = gson.fromJson(str4, Person.class); System.out.println(personA); // Person [id=123, name=sisi, place=BeiJing] Map<String,Object> mapA = gson.fromJson(str4, Map.class); System.out.println(mapA); // {id=123.0, name=sisi, place=BeiJing} //在已经知道要转成List的情况下可以直接带List.class参数 List<Map> list = gson.fromJson(str5 /*str2*/, List.class); for(Map map : list){ String s = gson.toJson(map); Person p =gson.fromJson(s, Person.class); System.out.println(p); } // Person [id=123, name=sisi, place=BeiJing] // Person [id=456, name=bobo, place=HangZhou] //转泛型的时候会把int转成double List<Map<String,Object>> list_map = gson.fromJson(str5, new TypeToken<List<Map<String,Object>>>(){}.getType()); for(Map m: list_map){ System.out.println(m); } // {id=123.0, name=sisi, place=BeiJing} // {id=456.0, name=bobo, place=HangZhou} //转Person可以把double的小数舍去 List<Person> personList = gson.fromJson(str2 /*str5*/, new TypeToken<List<Person>>(){}.getType()); for(Person p: personList){ System.out.println(p); } // Person [id=123, name=sisi, place=BeiJing] // Person [id=2002, name=bobo, place=HangZhou] } }
gson格式化-美化
Gson gson = new GsonBuilder().setPrettyPrinting().create(); String gsonInfo = gson.toJson(map);
我的其它相关文章
以下为特殊处理:
- Json的日期格式化(gson) -- 强力推荐
- Json的日期格式化(json-lib) -- 庸俗至极
google工具类的其它用法
java 驼峰字符和下划线字符相互转换工具类==>https://blog.csdn.net/Reminisce_man/article/details/77017554
感觉空虚寂寞,只是因为你无所关注,无处付出。