Convert between objects and JSON strings using Google's Gson

Source: Internet
Author: User
Tags tojson

Convert between objects and JSON strings using Google's Gson

Need Gson.jar

1.jsonutil.java

Java code
  1. Package Com.snail.json;
  2. Import Java.lang.reflect.Type;
  3. Import Com.google.gson.Gson;
  4. /**
  5. * Java object and JSON string Mutual conversion tool class
  6. * @author Penghuaiyi
  7. * @date 2013-08-10
  8. */
  9. Public Final class Jsonutil {
  10. Private Jsonutil () {}
  11. /** 
  12. * Object converted to JSON string
  13. * @param obj
  14. * @return
  15. */
  16. public static String ToJson (Object obj) {
  17. Gson Gson = new Gson ();
  18. return Gson.tojson (obj);
  19. }
  20. /** 
  21. * JSON strings are converted to objects
  22. * @param str
  23. * @param type
  24. * @return
  25. */
  26. public static <T> T Fromjson (String str, type type) {
  27. Gson Gson = new Gson ();
  28. return Gson.fromjson (str, type);
  29. }
  30. /** 
  31. * JSON strings are converted to objects
  32. * @param str
  33. * @param type
  34. * @return
  35. */
  36. public static <T> T Fromjson (String str, class<t> type) {
  37. Gson Gson = new Gson ();
  38. return Gson.fromjson (str, type);
  39. }
  40. }

2. Person.java

Java code
  1. Package Com.snail.json;
  2. Public class Person {
  3. private int id;
  4. private String name;
  5. public int getId () {
  6. return ID;
  7. }
  8. public void setId (int id) {
  9. this.id = ID;
  10. }
  11. Public String GetName () {
  12. return name;
  13. }
  14. public void SetName (String name) {
  15. this.name = name;
  16. }
  17. }

3. Test.java

Java code
  1. Package Com.snail.json;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashMap;
  4. Import java.util.List;
  5. Import Java.util.Map;
  6. Import Java.util.Map.Entry;
  7. Import Com.google.gson.reflect.TypeToken;
  8. Public class Test {
  9. /** 
  10. * Conversion of General objects
  11. */
  12. public static void Objecttojson () {
  13. Person person = new Person ();
  14. Person.setid (1);
  15. Person.setname ("one");
  16. //javabean Convert to JSON string
  17. String jsonstr = Jsonutil.tojson (person);
  18. System.out.println (JSONSTR);
  19. //json string converted to JavaBean
  20. Person Newperson = Jsonutil.fromjson (jsonstr, person.   Class);
  21. System.out.println (person = = Newperson);
  22. System.out.println (Newperson.getid () +"," +newperson.getname ());
  23. }
  24. /** 
  25. * Composite structure Data conversion (List)
  26. */
  27. public static void Listtojson () {
  28. Person Person1 = new Person ();
  29. Person1.setid (1);
  30. Person1.setname ("one");
  31. Person Person2 = new Person ();
  32. Person2.setid (2);
  33. Person2.setname ("both");
  34. list<person> list = new arraylist<person> ();
  35. List.add (Person1);
  36. List.add (Person2);
  37. //javabean Convert to JSON string
  38. String jsonstr = Jsonutil.tojson (list);
  39. System.out.println (JSONSTR);
  40. //json strings into JavaBean objects
  41. List<person> RTN = Jsonutil.fromjson (jsonstr, new typetoken<list<person>> () {}.getType ());
  42. For (person Person:rtn) {
  43. System.out.println (Person.getid () +"," +person.getname ());
  44. }
  45. }
  46. /** 
  47. * Composite structure Data conversion (MAP)
  48. */
  49. public static void Maptojson () {
  50. Person Person1 = new Person ();
  51. Person1.setid (1);
  52. Person1.setname ("one");
  53. Person Person2 = new Person ();
  54. Person2.setid (2);
  55. Person2.setname ("both");
  56. map<integer,person> map = new hashmap<integer,person> ();
  57. Map.put (Person1.getid (), Person1);
  58. Map.put (Person2.getid (), Person2);
  59. //javabean Convert to JSON string
  60. String jsonstr = Jsonutil.tojson (map);
  61. System.out.println (JSONSTR);
  62. //json strings into map objects
  63. Map<integer,person> RTN = Jsonutil.fromjson (jsonstr, new Typetoken<map<integer,person>> () {}.  GetType ());
  64. for (Entry<integer, person> entry:rtn.entrySet ()) {
  65. Integer key = Entry.getkey ();
  66. Person Newperson = Entry.getvalue ();
  67. System.out.println ("key:" +key+"," +newperson.getid () +"," +newperson.getname () ");
  68. }
  69. }
  70. public static void Main (string[] args) {
  71. Objecttojson ();
  72. System.out.println ("****************************");
  73. Listtojson ();
  74. System.out.println ("****************************");
  75. Maptojson ();
  76. }
  77. }

Convert between objects and JSON strings using Google's Gson

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.