How to convert Java object to JSON using GSON

In this tutorial we will show you how to convert Java object to JSON using GSON.

Setup:

You will require the following maven dependancy:

You will also need to create the following Java class to represent the JSON string:

How to convert Java object to JSON using GSON

Now that we have our setup complete, we can now look at converting the LibraryUser object to a JSON string representation using GSON. Below is an example of how this can be achieved.

Output:

Running the above code produces the following output:

In conclusion I hope you have found this post useful and it helped you solve your development issue.

Similar posts:

  1. How to convert JSON string to a Java object
  2. How to write JSON object to File in Java
  3. How to parse a JSON file using JSON
  4. How to convert InputStream to String in Java
  5. How to create a file in Java

References:

  1. GSON documentation

 

Leave a Reply

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