I have api its response like this:
{
"0": "_serialize",
"1": [
"login"
],
"users": {
"token": "aaaaa",
"message": "login successful."
}
}
How can I parse this in android using Gson ?
I have api its response like this:
{
"0": "_serialize",
"1": [
"login"
],
"users": {
"token": "aaaaa",
"message": "login successful."
}
}
How can I parse this in android using Gson ?
Create a pojo of your Json. you can do so with many resources available online like this or this.
Now use below format.
Gson gson = new Gson();
MyPojo myPojo = new MyPojo();
Type collectionType = new TypeToken<MyPojo>() {
}.getType();
myPojo = gson.fromJson(responseJson,
collectionType);
Hope this helps.
First of all make model classes from your json using tools like this one. Just copy your json and get the objects.
Let's suppose you call the class Model
Then use this code
to get object from your json.
String json = "that-json";
Model m = gson.fromJson(json, Model.class);
More on Gson's guides.
Generate the Model class from the response
public void onResponse(String response) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
YourModelClass objModel = gson.fromJson(jsonObject.getJSONObject("data").toString(), YourModelClass.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
use gson and map your response to Java Model class.
This will be your model class ->
User.java Model class.
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Users implements Serializable
{
@SerializedName("token")
@Expose
private String token;
@SerializedName("message")
@Expose
private String message;
private final static long serialVersionUID = 3387741697269012981L;
/**
* No args constructor for use in serialization
*
*/
public Users() {
}
/**
*
* @param message
* @param token
*/
public Users(String token, String message) {
super();
this.token = token;
this.message = message;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Users withToken(String token) {
this.token = token;
return this;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Users withMessage(String message) {
this.message = message;
return this;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("token", token).append("message", message).toString();
}
}
this is your root/parent class, from where parsing starts.
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Root implements Serializable
{
@SerializedName("0")
@Expose
private String _0;
@SerializedName("1")
@Expose
private List<String> _1 = null;
@SerializedName("users")
@Expose
private Users users;
private final static long serialVersionUID = 5880229946098431789L;
/**
* No args constructor for use in serialization
*
*/
public Example() {
}
/**
*
* @param users
* @param _0
* @param _1
*/
public Example(String _0, List<String> _1, Users users) {
super();
this._0 = _0;
this._1 = _1;
this.users = users;
}
public String get0() {
return _0;
}
public void set0(String _0) {
this._0 = _0;
}
public Example with0(String _0) {
this._0 = _0;
return this;
}
public List<String> get1() {
return _1;
}
public void set1(List<String> _1) {
this._1 = _1;
}
public Example with1(List<String> _1) {
this._1 = _1;
return this;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public Example withUsers(Users users) {
this.users = users;
return this;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("_0", _0).append("_1", _1).append("users", users).toString();
}
}
now at the code where you receive Json map json to this root/parent class from where parsing starts.
Root root = new Gson().fromJson(/*put your json response variable here*/, Root.class);
and use root object to fetch/set any data via public get methods.