How do I convert Map into JSON?

This example show you how to convert a java.util.Map into JSON string and back to Map again.

Here is the result of the program:

How do I convert collections into JSON?

This example show you how to convert Java collections object into JSON string. For Student class use in this example you can find it the previous example on How do I convert object into JSON?.

Here is the result of our program:

How do I convert array into JSON?

In the example below you can see how to convert an array into JSON string. We serialize the array to JSON using the Gson.toJson() method. To deserialize a string of JSON into array we use the Gson.fromJson() method.

Here is our code result: