Created
October 6, 2021 18:53
-
-
Save codinginflow/fc3f049e594ab160fd5a3e760dabc2ea to your computer and use it in GitHub Desktop.
GSON Tutorial Part 4
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.Expose; | |
public class Employee { | |
@Expose | |
private String firstName; | |
@Expose(serialize = false) | |
private int age; | |
@Expose(deserialize = false) | |
private String mail; | |
private String password; | |
public Employee(String firstName, int age, String mail, String password) { | |
this.firstName = firstName; | |
this.age = age; | |
this.mail = mail; | |
this.password = password; | |
} | |
} |
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.GsonBuilder; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); | |
Employee employee = new Employee( | |
"John", | |
30, | |
"john@mail.com", | |
"fdfarg2" | |
); | |
String jsonResult = gson.toJson(employee); | |
String json = "{\"age\":30,\"firstName\":\"John\",\"mail\":\"john@mail.com\",\"password\":\"fdfarg2\"}"; | |
Employee employee1 = gson.fromJson(json, Employee.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment