-
-
Save biswa-rx/84710a964475f7fc1553ddf8122b9b1b to your computer and use it in GitHub Desktop.
GSON Tutorial Part 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.gsonexample; | |
import com.google.gson.annotations.SerializedName; | |
public class Address { | |
@SerializedName("country") | |
private String mCountry; | |
@SerializedName("city") | |
private String mCity; | |
public Address(String country, String city) { | |
mCountry = country; | |
mCity = city; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.gsonexample; | |
import com.google.gson.annotations.SerializedName; | |
import java.util.List; | |
public class Employee { | |
@SerializedName("first_name") | |
private String mFirstName; | |
@SerializedName("age") | |
private int mAge; | |
@SerializedName("mail") | |
private String mMail; | |
@SerializedName("address") | |
private Address mAddress; | |
@SerializedName("family") | |
private List<FamilyMember> mFamily; | |
public Employee(String firstName, int age, String mail, Address address, List<FamilyMember> family) { | |
mFirstName = firstName; | |
mAge = age; | |
mMail = mail; | |
mAddress = address; | |
mFamily = family; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"address": { | |
"city": "Berlin", | |
"country": "Germany" | |
}, | |
"age": 30, | |
"family": [ | |
{ | |
"age": 30, | |
"role": "Wife" | |
}, | |
{ | |
"age": 5, | |
"role": "Daughter" | |
} | |
], | |
"first_name": "John", | |
"mail": "john@gmail.com" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.gsonexample; | |
import com.google.gson.annotations.SerializedName; | |
public class FamilyMember { | |
@SerializedName("role") | |
private String mRole; | |
@SerializedName("age") | |
private int mAge; | |
public FamilyMember(String role, int age) { | |
mRole = role; | |
mAge = age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.gsonexample; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Gson gson = new Gson(); | |
/* | |
Address address = new Address("Germany", "Berlin"); | |
List<FamilyMember> family = new ArrayList<>(); | |
family.add(new FamilyMember("Wife", 30)); | |
family.add(new FamilyMember("Daughter", 5)); | |
Employee employee = new Employee("John", 30, "john@gmail.com", address, family); | |
String json = gson.toJson(family); | |
*/ | |
String json = "[{\"age\":30,\"role\":\"Wife\"},{\"age\":5,\"role\":\"Daughter\"}]"; | |
Type familyType = new TypeToken<ArrayList<FamilyMember>>() {}.getType(); | |
ArrayList<FamilyMember> family = gson.fromJson(json, familyType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment