Skip to content

Gson – Convert JSON to Java object

Gson – Convert JSON to Java object example shows how to convert JSON to a Java object using the Gson library in Java.

How to convert JSON to Java object?

It is fairly easy to convert JSON to a Java object using the GSON library. We need to use the fromJson method of the Gson class to convert JSON to a POJO class.

The fromJson method converts the string JSON data to the given class object.

Here is a simple example to demonstrate that.

Output

We can also read a JSON from a file and convert it to a Java object as given below. Here is the employee.json file content that we are going to read and convert to an Employee object.

Output

We can also read JSON from a URL and convert it to a Java object as given below.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.