As new libraries start to emerge, it seems that they tend to make our lives as Android developers easier.GSON is one of those libraries, it's main functionality is that it can be used to serialize Java Objects into JSON string representation and vice versa, for deserializing JSON strings into Java Objects.
This example is divided into four steps:
- Adding the library to Android
- JSON String to POJO class
- Converting the JSON String to Java Object and vice versa
- Complete code
We’ll start of with a simple tutorial on how to integrate and use the GSON Library in Android.
STEP 1 Adding the library to Android
STEP 1 Adding the library to Android
Open the build.gradle file which is in the app Module and add the following line in side dependencies block:
dependencies {
…
compile 'com.google.code.gson:gson:2.5'
}
STEP 2 JSON String to POJO class
STEP 2 JSON String to POJO class
Let's say for example we have the following JSON String:
{
"users":[
{
"users":[
{
"id":"1",
"phone":"111222333",
"image":"2014-12-13-08-08-29.jpg",
"name":"john",
"surname":"doe"
},
{
"id":"2",
"phone":"111222444",
"image":"2014-12-13-08-09-29.jpg",
"name":"anon",
"surname":"nona"
},
{
"id":"3",
"phone":"111222555",
"image":"2014-12-13-08-10-29.jpg",
"name":"filan",
"surname":"fisteku"
}
]
}
Which is a JSON array, now first we need to parse/serialize the JSON to java objects with the classes that represent the data. So open this website
which creates the classes for us, you can copy and paste the JSON into the websites editor, on the right side select Source type: JSON and Annotation style: GSON.At the bottom of the page click the preview button, this will generate the classes which we can create in Java and replace it with the code in the preview
Now first create an Example class and a User class in your android project then replace them with the code in the Preview from the website. They should look like the code below: NOTE delete these two lines:
import javax.annotation.Generated;
@Generated("org.jsonschema2pojo")
First the Example class:
package testgson.gsontest;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("users")
@Expose
private List<User> users = new ArrayList<User>();
/**
*
* @return
* The users
*/
public List<User> getUsers() {
return users;
}
/**
*
* @param users
* The users
*/
public void setUsers(List<User> users) {
this.users = users;
}
}
Second the User class:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("id")
@Expose
private String id;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("image")
@Expose
private String image;
@SerializedName("name")
@Expose
private String name;
@SerializedName("surname")
@Expose
private String surname;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The phone
*/
public String getPhone() {
return phone;
}
/**
*
* @param phone
* The phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
*
* @return
* The image
*/
public String getImage() {
return image;
}
/**
*
* @param image
* The image
*/
public void setImage(String image) {
this.image = image;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The surname
*/
public String getSurname() {
return surname;
}
/**
*
* The surname
*/
public void setSurname(String surname) {
this.surname = surname;
}
}
STEP 3 Converting the JSON String to Java Object and vice versa
Finally to actually convert/serialize your JSON to a java object use the code below:
Example example = new Gson().fromJson(json,Example.class);
And to convert your object to a JSON string use the code below:
String json = new Gson().toJson(example);
Complete code:
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = "{\n" +
" \"users\":[ \n" +
" { \n" +
" \"id\":\"1\",\n" +
" \"phone\":\"111222333\",\n" +
" \"image\":\"2014-12-13-08-08-29.jpg\",\n" +
" \"name\":\"john\",\n" +
" \"surname\": \"doe\"\n" +
" },\n" +
" { \n" +
" \"id\":\"2\",\n" +
" \"phone\":\"111222444\",\n" +
" \"image\":\"2014-12-13-08-09-29.jpg\",\n" +
" \"name\":\"anon\",\n" +
" \"surname\": \"nona\"\n" +
" },\n" +
" { \n" +
" \n" +
" \"id\":\"3\",\n" +
" \"phone\":\"111222555\",\n" +
" \"image\":\"2014-12-13-08-10-29.jpg\",\n" +
" \"name\":\"filan\",\n" +
" \"surname\": \"fisteku\"\n" +
" }\n" +
"]\n" +
"}";
Example example = new Gson().fromJson(json,Example.class);
System.out.println(example.getUsers().get(0).getName());
}
}
Example.java class
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("users")
@Expose
private List<User> users = new ArrayList<User>();
/**
*
* @return
* The users
*/
public List<User> getUsers() {
return users;
}
/**
*
* @param users
* The users
*/
public void setUsers(List<User> users) {
this.users = users;
}
}
User.java class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("id")
@Expose
private String id;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("image")
@Expose
private String image;
@SerializedName("name")
@Expose
private String name;
@SerializedName("surname")
@Expose
private String surname;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The phone
*/
public String getPhone() {
return phone;
}
/**
*
* @param phone
* The phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
*
* @return
* The image
*/
public String getImage() {
return image;
}
/**
*
* @param image
* The image
*/
public void setImage(String image) {
this.image = image;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The surname
*/
public String getSurname() {
return surname;
}
/**
*
* @param surname
* The surname
*/
public void setSurname(String surname) {
this.surname = surname;
}
}
0 comments :
Post a Comment