Tutorial by Examples

The example shows parsing a JSON object using the Gson library from Google. Parsing objects: class Robot { //OPTIONAL - this annotation allows for the key to be different from the field name, and can be omitted if key and field name are same . Also this is good coding practice as it decouple ...
If you want to parse a String to enum with Gson: {"status" : "open"} public enum Status { @SerializedName("open") OPEN, @SerializedName("waiting") WAITING, @SerializedName("confirm") CONFIRM, @SerializedName(&...
Method 1 Gson gson = new Gson(); String json = "[ \"Adam\", \"John\", \"Mary\" ]"; Type type = new TypeToken<List<String>>(){}.getType(); List<String> members = gson.fromJson(json, type); Log.v("Members", members.toString());...
Import in your gradle root file classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' Import in your gradle app file apt 'com.google.auto.value:auto-value:1.2' apt 'com.ryanharter.auto.value:auto-value-gson:0.3.1' provided 'com.jakewharton.auto.value:auto-value-annotations:1.2-update...
Suppose we have a JSON string: ["first","second","third"] We can parse this JSON string into a String array : Gson gson = new Gson(); String jsonArray = "[\"first\",\"second\",\"third\"]"; String[] strings = gson.fromJson(j...
dependencies { compile 'com.google.code.gson:gson:2.8.1' } To use latest version of Gson The below line will compile latest version of gson library everytime you compile, you do not have to change version. Pros: You can use latest features, speed and less bugs. Cons: It might break com...
This will load a JSON file from disk and convert it to the given type. public static <T> T getFile(String fileName, Class<T> type) throws FileNotFoundException { Gson gson = new GsonBuilder() .create(); FileReader json = new FileReader(fileName); return gson....
Sometimes you need to serialize or deserialize some fields in a desired format, for example your backend may use the format "YYYY-MM-dd HH:mm" for dates and you want your POJOS to use the DateTime class in Joda Time. In order to automatically convert these strings into DateTimes object, y...
First of all you need to add the GsonConverterFactory to your build.gradle file compile 'com.squareup.retrofit2:converter-gson:2.1.0' Then, you have to add the converter factory when creating the Retrofit Service: Gson gson = new GsonBuilder().create(); new Retrofit.Builder() .baseUrl...
Suppose we have a json : { "total_count": 132, "page_size": 2, "page_index": 1, "twitter_posts": [ { "created_on": 1465935152, "tweet_id": 210462857140252672, "tweet": "Along with our ne...
Imagine you have all dates in all responses in some custom format, for instance /Date(1465935152)/ and you want apply general rule to deserialize all Json dates to java Date instances. In this case you need to implement custom Json Deserializer. Example of json: { "id": 1, "cr...
Gson does not support inheritance out 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 getI...

Page 1 of 1