Convert object to/from json using Gson

Posted on Updated on

Google GSON is JSON parser and also generator for java, which is widely use to convert object to/from Json. To learn more about Gson one can refer to Gson tutorial. GSON library is used to convert any object to Json and vice-versa. Here in this tutorial we’ll be discussing below popular examples to convert different object to/from Json.

  1. Convert Object to/from Json.
  2. Convert List of objects to/from Json.
  3. Convert Map containing objects.

Simple POJO which will be used as an object in all examples.

1) Convert Object to/from Json

  • Example to convert Emp object to Json.

    Output

  • Example to convert Json to Emp object.

    Output

2) Convert List of objects to/from Json

  • Example to convert List of Emp objects to Json.

    Output

  • Example to convert Json to List of Emp objects.

    Json object in user readable format.

    Output

3) Convert Map containing objects.

  • Example to convert Map containing Emp objects to Json.

    Output

  • Example to convert Json to Map.

    Json String in user readable format

    Output


    Stay tuned for more tutorials and examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.