Skip to content

Instantly share code, notes, and snippets.

@biswa-rx
Forked from codinginflow/Employee.java
Created April 27, 2023 13:35
Show Gist options
  • Save biswa-rx/5b1e5da6b08fb395f16b8b58ddc83661 to your computer and use it in GitHub Desktop.
Save biswa-rx/5b1e5da6b08fb395f16b8b58ddc83661 to your computer and use it in GitHub Desktop.
GSON Tutorial Part 1
package com.codinginflow.gsonexample;
import com.google.gson.annotations.SerializedName;
public class Employee {
@SerializedName("first_name")
private String mFirstName;
@SerializedName("age")
private int mAge;
@SerializedName("mail")
private String mMail;
public Employee(String firstName, int age, String mail) {
mFirstName = firstName;
mAge = age;
mMail = mail;
}
}
{
"age": 30,
"first_name": "John",
"mail": "john@gmail.com"
}
package com.codinginflow.gsonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
/*
Employee employee = new Employee("John", 30, "john@gmail.com");
String json = gson.toJson(employee);
*/
String json = "{\"first_name\":\"John\",\"age\":30,\"mail\":\"john@gmail.com\"}";
Employee employee = gson.fromJson(json, Employee.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment