Tutorial by Examples

In order to use Gson you have to include it in your project. You can do this by adding the following dependency of the Gson version available in Maven Central: Maven Add to pom.xml <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <a...
Gson gson = new Gson(); //Create a Gson object MyType target = new MyType(); //This is the object you want to convert to JSON String json = gson.toJson(target); // serializes target to Json MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
JSON: [ { "id": 8484, "name": "David", "height": 173.2, "weight": 75.42 }, { "id": 8485, "name": "Ronald", "height": 183.73, "weight": 83.1 } ] ...
The Gson library provides Gson.class which handles all conversion between Java and JSON objects. An instance of this class can be created by invoking default constructor. You usually would like to have one Gson instance for the most part of operations in your program. Gson gson = new Gson(); Fir...
String jsonStr = "{\"name\" : \"Abcd\", \"greeting\": \"Hello\", }"; //Sample Json String Gson gson = new Gson(); // Creates new instance of Gson JsonElement element = gson.fromJson (jsonStr, JsonElement.class); //Converts the json string to Json...
GSON does not support inheritance our of the box. Let's say we have the following class hierarchy: public class BaseClass { int a; public int getInt() { return a; } } public class DerivedClass1 extends BaseClass { int b; @Override public int getIn...

Page 1 of 1