Last active
April 27, 2023 13:35
-
-
Save codinginflow/d2c84b5e667e70d18b27f718027c42e4 to your computer and use it in GitHub Desktop.
GSON Tutorial Part 2
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; | |
public class Employee { | |
@SerializedName("first_name") | |
private String mFirstName; | |
@SerializedName("age") | |
private int mAge; | |
@SerializedName("mail") | |
private String mMail; | |
@SerializedName("address") | |
private Address mAddress; | |
public Employee(String firstName, int age, String mail, Address address) { | |
mFirstName = firstName; | |
mAge = age; | |
mMail = mail; | |
mAddress = address; | |
} | |
} |
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, | |
"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 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(); | |
/* | |
Address address = new Address("Germany", "Berlin"); | |
Employee employee = new Employee("John", 30, "john@gmail.com", address); | |
String json = gson.toJson(employee); | |
*/ | |
String json = "{\"address\":{\"city\":\"Berlin\",\"country\":\"Germany\"},\"age\":30,\"first_name\":\"John\",\"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