Skip to content

Gson – Convert String to JSON example

Gson – Convert String to JSON example shows how to convert string to JSON using the Gson library in Java. There are basically two ways in which we can convert any string containing JSON data to a JSON object.

1. Using JSONParser class

This approach uses the parseString method of the JSONParser class to create a JSONObject from the string containing JSON data as given below.


2. Using fromJson method

The alternative approach is to use the fromJson method of the Gson class as given below.


Instead of providing the generic JsonObject class in the fromJson method, we can also provide the Employee pojo class if needed.

We can use either of the above given two approaches to convert a string to a JSON object using the Gson library.

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

About the author

Leave a Reply

Your email address will not be published.