Package COM. wangqsh. gson; import Java. util. hashmap; import Java. util. list; import Java. util. arraylist; import Java. util. map; import COM. google. gson. gson; import COM. google. gson. reflect. typetoken; public class gsontest {public static void main (string ARGs []) {system. out. println ("hello"); primitivesserialization (); primitivesdeserialization (); gson = new gson (); // object jsonobj = new bagofprimitives (); string JSON = gson. tojson (OBJ); system. out. println ("Object example"); system. out. println (JSON); OBJ = gson. fromjson (JSON, bagofprimitives. class); system. out. println (obj. tostring (); JSON = "{\" forecast \ ": [{\" order_id \ ": \" 2012102917 \ ", \" jingdu \": \ "108.1 \", \ "weidu \": \ "21.7 \", \ "time_dif \": 0, \ "wind_speed \": \ "15 \", \ "air_pressure \": \ "1,004 \", \ "radius_30k \": 0, \ "radius_50k \": 0, \ "typhoon_id \": \ "1223 \", \ "fc_station \": \ "1 \", \ "ty_date \": \ "2012-10-29 17:00 \"}]} "; Map <string, string> map; map = new hashmap <string, string> (); map. put ("city", "Beijing"); map. put ("cityid", "101010100"); system. out. println ("map's JSON:" + gson. tojson (MAP); Map <string, Map <string, string> weather; weather = new hashmap <string, Map <string, string> (); weather. put ("weatherinfo", MAP); system. out. println ("Weather info's JSON:" + gson. tojson (weather); string weatherinfo = gson. tojson (weather); weather = gson. fromjson (weatherinfo, new typetoken <Map <string, Map <string, string >> (){}. getType (); system. out. println (weather. tostring (); system. out. println ("Weather info's JSON:" + gson. tojson (weather); List <Map <string, string> List = new arraylist <Map <string, string> (); list. add (MAP); list. add (MAP); weatherinfo = gson. tojson (list); system. out. println (weatherinfo); Map <string, list <Map <string, string> listmap; listmap = new hashmap <string, list <Map <string, string >>> (); listmap. put ("weatherinfo", list); listmap. put ("path", list); system. out. println ("list map's JSON:" + gson. tojson (listmap); listmap = gson. fromjson (JSON, new typetoken <Map <string, list <Map <string, string >>> (){}. getType (); system. out. println ("listmap:" + listmap. tostring (); List <Map <string, list <Map <string, string >>> typhoon = new arraylist <Map <string, list <Map <string, string >>>> (); typhoon. add (listmap); typhoon. add (listmap); system. out. println ("typhoon's JSON:" + gson. tojson (typhoon); JSON = "{\" History \ ": [{\" jingdu \ ": \" 127.5 \ ", \" weidu \": \ "8.9 \", \ "wind_speed \": \ "18 \", \ "air_pressure \": \ "998 \", \ "dot_order \": 1, \ "ty_data \": \ "2012-10-24 \" },{ \ "jingdu \": \ "126.6 \", \ "weidu \": \ "9.1 \", \ "wind_speed \": \ "18 \", \ "air_presure \": \ "998 \", \ "dot_order \": 2, \ "ty_data \": \ "\" },{\ "jingdu \": \ "125.6 \", \ "weidu \": \ "10.8 \", \ "wind_speed \": \ "20 \", \ "air_pressure \": \ "995 \", \ "dot_order \": 3, \ "ty_data \": \ "2012-10-24 \"}, {\ "jingdu \": \ "124.3 \", \ "weidu \": \ "11.6 \", \ "wind_speed \": \ "20 \", \ "air_pressure \": \ "995 \", \ "dot_order \": 4, \ "ty_data \": \ "2012-10-24 20:00 \"}, {\ "jingdu \": \ "122.6 \", \ "weidu \": \ "12.0 \", \ "wind_speed \": \ "20 \", \ "air_pressure \": \ "995 \", \ "dot_order \": 5, \ "ty_data \": \ "2012-10-25 Nov 00 \"}, {\ "jingdu \": \ "122.0 \", \ "weidu \": \ "12.1 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 6, \ "ty_data \": \ "2012-10-25 05:00 \"}, {\ "jingdu \": \ "121.2 \", \ "weidu \": \ "12.6 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 7, \ "ty_data \": \ "2012-10-25 08:00 \"}, {\ "jingdu \": \ "120.1 \", \ "weidu \": \ "13.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 8, \ "ty_data \": \ "2012-10-25 11: 00 \"}, {\ "jingdu \": \ "119.3 \", \ "weidu \": \ "14.1 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 9, \ "ty_data \": \ "2012-10-25 \"}, {\ "jingdu \": \ "118.6 \", \ "weidu \": \ "14.2 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 10, \ "ty_data \": \ "2012-10-25 17:00 \"}, {\ "jingdu \": \ "118.0 \", \ "weidu \": \ "14.4 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 11, \ "ty_data \": \ "2012-10-25 20:00 \"}, {\ "jingdu \": \ "118.0 \", \ "weidu \": \ "14.4 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 12, \ "ty_data \": \ "2012-10-25 20:00 \"}, {\ "jingdu \": \ "117.5 \", \ "weidu \": \ "14.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 13, \ "ty_data \": \ "2012-10-25 23:00 \"}, {\ "jingdu \": \ "117.4 \", \ "weidu \": \ "14.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "992 \", \ "dot_order \": 14, \ "ty_data \": \ "2012-10-26 \"}, {\ "jingdu \": \ "116.6 \", \ "weidu \": \ "14.5 \", \ "wind_speed \": \ "25 \", \ "air_pressure \": \ "988 \", \ "dot_order \": 15, \ "ty_data \": \ "2012-10-26 05:00 \"}, {\ "jingdu \": \ "115.7 \", \ "weidu \": \ "14.6 \", \ "wind_speed \": \ "25 \", \ "air_pressure \": \ "988 \", \ "dot_order \": 16, \ "ty_data \": \ "2012-10-26 08:00 \"}, {\ "jingdu \": \ "115.0 \", \ "weidu \": \ "14.7 \", \ "wind_speed \": \ "28 \", \ "air_pressure \": \ "985 \", \ "dot_order \": 17, \ "ty_data \": \ "2012-10-26 11: 00 \"}, {\ "jingdu \": \ "114.4 \", \ "weidu \": \ "14.8 \", \ "wind_speed \": \ "28 \", \ "air_pressure \": \ "985 \", \ "dot_order \": 18, \ "ty_data \": \ "2012-10-26 \"}, {\ "jingdu \": \ "114.1 \", \ "weidu \": \ "15.1 \", \ "wind_speed \": \ "30 \", \ "air_pressure \": \ "980 \", \ "dot_order \": 19, \ "ty_data \": \ "2012-10-26 17:00 \"}, {\ "jingdu \": \ "113.7 \", \ "weidu \": \ "15.5 \", \ "wind_speed \": \ "30 \", \ "air_pressure \": \ "980 \", \ "dot_order \": 20, \ "ty_data \": \ "2012-10-26 20:00 \"}, {\ "jingdu \": \ "113.1 \", \ "weidu \": \ "15.8 \", \ "wind_speed \": \ "30 \", \ "air_pressure \": \ "980 \", \ "dot_order \": 21, \ "ty_data \"}, {\ "jingdu \": \ "112.4 \", \ "weidu \": \ "16.0 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 22, \ "ty_data \": \ "2012-10-27 \"}, {\ "jingdu \": \ "111.7 \", \ "weidu \": \ "16.5 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 23, \ "ty_data \": \ "2012-10-27 05:00 \"}, {\ "jingdu \": \ "110.9 \", \ "weidu \": \ "16.7 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 24, \ "ty_data \": \ "2012-10-27 08:00 \"}, {\ "jingdu \": \ "110.2 \", \ "weidu \": \ "16.8 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 25, \ "ty_data \": \ "2012-10-27 11: 00 \"}, {\ "jingdu \": \ "109.8 \", \ "weidu \": \ "16.9 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 26, \ "ty_data \": \ "2012-10-27 13:00 \"}, {\ "jingdu \": \ "109.6 \", \ "weidu \": \ "17.0 \", \ "wind_speed \": \ "35 \", \ "air_pressure \": \ "970 \", \ "dot_order \": 27, \ "ty_data \": \ "2012-10-27 \"}, {\ "jingdu \": \ "109.3 \", \ "weidu \": \ "17.1 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "962 \", \ "dot_order \": 28, \ "ty_data \": \ "2012-10-27 17:00 \"}, {\ "jingdu \": \ "109.3 \", \ "weidu \": \ "17.1 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "962 \", \ "dot_order \": 29, \ "ty_data \": \ "2012-10-27 17:00 \"}, {\ "jingdu \": \ "108.5 \", \ "weidu \": \ "17.8 \", \ "wind_speed \": \ "45 \", \ "air_pressure \": \ "950 \", \ "dot_order \": 30, \ "ty_data \": \ "2012-10-27 20:00 \"}, {\ "jingdu \": \ "108.5 \", \ "weidu \": \ "17.8 \", \ "wind_speed \": \ "45 \", \ "air_pressure \": \ "950 \", \ "dot_order \": 31, \ "ty_data \"}, {\ "jingdu \": \ "107.7 \", \ "weidu \": \ "18.3 \", \ "wind_speed \": \ "42 \", \ "air_pressure \": \ "955 \", \ "dot_order \": 32, \ "ty_data \": \ "2012-10-28 \"}, {\ "jingdu \": \ "107.7 \", \ "weidu \": \ "18.3 \", \ "wind_speed \": \ "42 \", \ "air_pressure \": \ "955 \", \ "dot_order \": 33, \ "ty_data \": \ "2012-10-28 05:00 \"}, {\ "jingdu \": \ "107.4 \", \ "weidu \": \ "18.4 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 34, \ "ty_data \": \ "2012-10-28 08:00 \"}, {\ "jingdu \": \ "107.1 \", \ "weidu \": \ "18.8 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 35, \ "ty_data \": \ "2012-10-28 11: 00 \"}, {\ "jingdu \": \ "107.1 \", \ "weidu \": \ "19.3 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 36, \ "ty_data \": \ "2012-10-28 \"}, {\ "jingdu \": \ "107.0 \", \ "weidu \": \ "19.5 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 37, \ "ty_data \": \ "2012-10-28 16:00 \"}, {\ "jingdu \": \ "106.9 \", \ "weidu \": \ "19.6 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 38, \ "ty_data \": \ "2012-10-28 17:00 \"}, {\ "jingdu \": \ "106.8 \", \ "weidu \": \ "19.9 \", \ "wind_speed \": \ "40 \", \ "air_pressure \": \ "960 \", \ "dot_order \": 39, \ "ty_data \": \ "2012-10-28 19:00 \"}, {\ "jingdu \": \ "106.7 \", \ "weidu \": \ "20.0 \", \ "wind_speed \": \ "38 \", \ "air_pressure \": \ "965 \", \ "dot_order \": 40, \ "ty_data \": \ "2012-10-28 20:00 \"}, {\ "jingdu \": \ "106.7 \", \ "weidu \": \ "20.1 \", \ "wind_speed \": \ "38 \", \ "air_pressure \": \ "965 \", \ "dot_order \": 41, \ "ty_data \": \ "2012-10-28 21:00 \"}, {\ "jingdu \": \ "106.6 \", \ "weidu \": \ "20.1 \", \ "wind_speed \": \ "38 \", \ "air_pressure \": \ "965 \", \ "dot_order \": 42, \ "ty_data \": \ "2012-10-28 \"}, {\ "jingdu \": \ "106.6 \", \ "weidu \": \ "20.2 \", \ "wind_speed \": \ "35 \", \ "air_pressure \": \ "970 \", \ "dot_order \": 43, \ "ty_data \": \ "2012-10-28 23:00 \"}, {\ "jingdu \": \ "106.6 \", \ "weidu \": \ "20.4 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 44, \ "ty_data \": \ "2012-10-29 0:00 \"}, {\ "jingdu \": \ "106.6 \", \ "weidu \": \ "20.6 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 45, \ "ty_data \"}, {\ "jingdu \": \ "106.7 \", \ "weidu \": \ "20.8 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 46, \ "ty_data \": \ "2012-10-29 \"}, {\ "jingdu \": \ "106.9 \", \ "weidu \": \ "20.9 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 47, \ "ty_data \": \ "2012-10-29 \"}, {\ "jingdu \": \ "107.0 \", \ "weidu \": \ "21.0 \", \ "wind_speed \": \ "33 \", \ "air_pressure \": \ "975 \", \ "dot_order \": 48, \ "ty_data \": \ "2012-10-29 05:00 \"}, {\ "jingdu \": \ "107.4 \", \ "weidu \": \ "21.1 \", \ "wind_speed \": \ "25 \", \ "air_pressure \": \ "985 \", \ "dot_order \": 49, \ "ty_data \"}, {\ "jingdu \": \ "107.6 \", \ "weidu \": \ "21.3 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "990 \", \ "dot_order \": 50, \ "ty_data \": \ "2012-10-29 08:00 \"}, {\ "jingdu \": \ "107.7 \", \ "weidu \": \ "21.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "990 \", \ "dot_order \": 51, \ "ty_data \": \ "2012-10-29 10:00 \"}, {\ "jingdu \": \ "107.7 \", \ "weidu \": \ "21.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "990 \", \ "dot_order \": 52, \ "ty_data \": \ "2012-10-29 11: 00 \"}, {\ "jingdu \": \ "107.7 \", \ "weidu \": \ "21.5 \", \ "wind_speed \": \ "23 \", \ "air_pressure \": \ "990 \", \ "dot_order \": 53, \ "ty_data \": \ "2012-10-29 11: 00 \"}, {\ "jingdu \": \ "108.1 \", \ "weidu \": \ "21.7 \", \ "wind_speed \": \ "15 \", \ "air_pressure \": \ "1,004 \", \ "dot_order \": 54, \ "ty_data \": \ "2012-10-29 17:00 \"}], \ "forecast \": [{\ "order_id \": \ "2012102917 \", \ "jingdu \": \ "108.1 \", \ "weidu \": \ "21.7 \", \ "time_dif \": 0, \ "wind_speed \": \ "15 \", \ "air_pressure \": \ "1,004 \", \ "radius_30k \": 0, \ "radius_50k \": 0, \ "typhoon_id \": \ "1223 \", \ "fc_station \": \ "1 \", \ "ty_date \": \ "2012-10-29 17:00 \"}]} "; listmap = gson. fromjson (JSON, new typetoken <Map <string, list <Map <string, string >>> (){}. getType (); system. out. println ("Typhoon:" + listmap);}/** primitive type serialization */public static void primitivesserialization () {gson = new gson (); // intstring result = gson. tojson (1); system. out. println (result); // stringsystem. out. println (gson. tojson ("ABCD"); int [] values = {1}; system. out. println (gson. tojson (values);}/** primitive type deserialization */public static void primitivesdeserialization () {gson = new gson (); // intint one = gson. fromjson ("1", Int. class); system. out. println (one); integer integerone = gson. fromjson ("1", integer. class); system. out. println (integerone); long lng = gson. fromjson ("1", long. class); system. out. println (LNG); string STR = gson. fromjson ("\" ABC \ "", String. class); system. out. println (STR );}}Package COM. wangqsh. gson; public class bagofprimitives {private int value = 1; private string value2 = "ABC"; private transient int value3 = 3; bagofprimitives () {// do nothing} Public String tostring () {return "value: 1 \ nvalue: 2 \ nvalue3: ABC ";}}
Below is the main program