google.son
输出:
{"id":1,"name":"llll","age":12,"pass":"sada"}
{"1":"A","2":"S","3":"F"}
++++++
{1=A, 2=S, 3=F}
{1=A, 2=S, 3=F}
[{"1":"A"},{"2":"AA"},{"3":"AD"}]
[2,3,4]
[1,2,3,4,4]
+++
[1, 2, 3, 4, 4]
[1, 2, 3, 4, 4]
[{"id":1,"name":"ll","age":12,"pass":"saddddda"},{"id":1,"name":"llssll","age":12,"pass":"saaaada"}]
+++++++++
[Person [id=1, name=ll, age=12, pass=saddddda], Person [id=1, name=llssll, age=12, pass=saaaada]]
[Person [id=1, name=ll, age=12, pass=saddddda], Person [id=1, name=llssll, age=12, pass=saaaada]]
-----------------
Person [id=1, name=llll, age=0, pass=sada]
"2016-7-5 17:04:55"
Tue Jul 05 17:04:55 CST 2016
import java.lang.reflect.Type; import com.google.gson.Gson; public class ObjectUtil { public static <T> T getObjFromJsonByClazz(String json,Class <T> classz){ Gson gson = new Gson(); T t = gson.fromJson(json, classz); return t; } public static String getJsonFromObj(Object classz){ Gson gson = new Gson(); return gson.toJson(classz); } public static <T> T getObjeFromJsonByType(String json,Type type){ Gson gson = new Gson(); T t = gson.fromJson(json, type); return t; } public static Gson getGosn(){ return new Gson(); } }
import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.reflect.TypeToken; import jdbc.model.Person; public class JsonTest { /* * {"id":1,"name":"llll","age":12,"pass":"sada"} {"1":"A","2":"S","3":"F"} * [{"1":"A"},{"2":"AA"},{"3":"AD"}] [2,3,4] [1,2,3,4,4] * [{"id":1,"name":"ll","age":12,"pass":"saddddda"},{"id":1,"name":"llssll", * "age":12,"pass":"saaaada"}] */ public static void main(String[] args) { testPerson(); testMap(); testList(); testListMap(); testArray(); testListPerson(); System.out.println("-----------------"); testfromPerson(); testDateBeanJson(); } private static void testfromPerson() { String s = "{'id':'1','name':'llll','pass':'sada'}"; Person map = ObjectUtil.getObjFromJsonByClazz(s, Person.class); System.out.println(map); } public static void testListPerson() { List<Person> list = new ArrayList<>(); Person p1 = new Person(); p1.setAge(12); p1.setId(1); p1.setName("ll"); p1.setPass("saddddda"); Person p2 = new Person(); p2.setAge(12); p2.setId(1); p2.setName("llssll"); p2.setPass("saaaada"); list.add(p1); list.add(p2); String s = ObjectUtil.getJsonFromObj(list); System.out.println(s); System.out.println("+++++++++"); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Person>>() { }.getType(); List<Person> list1 = new ArrayList<>(); list1 = ObjectUtil.getGosn().fromJson(s, type); System.out.println(list1); List<Person> list2 = new ArrayList<>(); list2 = ObjectUtil.getObjeFromJsonByType(s, type); System.out.println(list2); } public static void testArray() { int[] arr = new int[] { 1, 2, 3, 4, 4 }; String s = ObjectUtil.getJsonFromObj(arr); System.out.println(s); System.out.println("+++"); Type type = new TypeToken<int[]>() { }.getType(); int[] arr1 ; arr1 = ObjectUtil.getGosn().fromJson(s, type); System.out.println(Arrays.toString(arr1)); int[] arr2; arr2 = ObjectUtil.getObjeFromJsonByType(s, type); System.out.println(Arrays.toString(arr2)); } public static void testListMap() { List<Integer> list = new ArrayList<>(); list.add(2); list.add(3); list.add(4); String s = ObjectUtil.getJsonFromObj(list); System.out.println(s); } public static void testList() { List<Map<Integer, String>> list = new ArrayList<>(); Map<Integer, String> map1 = new HashMap<>(); map1.put(1, "A"); Map<Integer, String> map2 = new HashMap<>(); map2.put(2, "AA"); Map<Integer, String> map3 = new HashMap<>(); map3.put(3, "AD"); list.add(map1); list.add(map2); list.add(map3); String s = ObjectUtil.getJsonFromObj(list); System.out.println(s); } public static void testMap() { Map<Integer, String> map = new HashMap<>(); map.put(1, "A"); map.put(2, "S"); map.put(3, "F"); String s = ObjectUtil.getJsonFromObj(map); System.out.println(s); System.out.println("++++++"); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<Integer, String>>() { }.getType(); Map<Integer, String> map1 = new HashMap<>(); map1 = ObjectUtil.getGosn().fromJson(s, type); System.out.println(map1); Map<Integer, String> map2 = new HashMap<>(); map2 = ObjectUtil.getObjeFromJsonByType(s, type); System.out.println(map2); } public static void testPerson() { Person p = new Person(); p.setAge(12); p.setId(1); p.setName("llll"); p.setPass("sada"); String pstr = ObjectUtil.getJsonFromObj(p); System.out.println(pstr); } public static void testDateBeanJson() { // 日期Bean数据 Date bean = new Date(); // Bean(带日期属性) -> Json String json = ObjectUtil.getGosn().toJson(bean); System.out.println(json); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Date>() { }.getType(); Date b = ObjectUtil.getGosn().fromJson(json, type); System.out.println(b); } }