Skip to content Skip to sidebar Skip to footer

How To Parse Json Response Using Gson Library In Android?

I have api its response like this: { '0': '_serialize', '1': [ 'login' ], 'users': { 'token': 'aaaaa', 'message': 'login successful.' } } How can I parse this in

Solution 1:

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.

Stringjson="that-json";
Modelm= gson.fromJson(json, Model.class);

More on Gson's guides.

Solution 2:

Create a pojo of your Json. you can do so with many resources available online like this or this.

Now use below format.

Gsongson=newGson();
MyPojomyPojo=newMyPojo();
TypecollectionType=newTypeToken<MyPojo>() {
                    }.getType();
myPojo = gson.fromJson(responseJson,
                            collectionType);

Hope this helps.

Solution 3:

Generate the Model class from the response

publicvoidonResponse(String response) {   
    JSONObject jsonObject = null;
    try {
        jsonObject = newJSONObject(response);

        YourModelClass objModel = gson.fromJson(jsonObject.getJSONObject("data").toString(), YourModelClass.class);

 } catch (JSONException e) {
    e.printStackTrace();
  }
}

Solution 4:

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;

publicclassUsersimplementsSerializable
{

    @SerializedName("token")
    @ExposeprivateString token;
    @SerializedName("message")
    @ExposeprivateString message;
    private final static long serialVersionUID = 3387741697269012981L;

    /**
     * No args constructor for use in serialization
     * 
     */publicUsers() {
    }

    /**
     * 
     * @parammessage
     * @paramtoken
     */publicUsers(String token, String message) {
        super();
        this.token = token;
        this.message = message;
    }

    publicStringgetToken() {
        return token;
    }

    publicvoidsetToken(String token) {
        this.token = token;
    }

    publicUserswithToken(String token) {
        this.token = token;
        returnthis;
    }

    publicStringgetMessage() {
        return message;
    }

    publicvoidsetMessage(String message) {
        this.message = message;
    }

    publicUserswithMessage(String message) {
        this.message = message;
        returnthis;
    }

    @OverridepublicStringtoString() {
        returnnewToStringBuilder(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;

publicclassRootimplementsSerializable
{

    @SerializedName("0")
    @ExposeprivateString _0;
    @SerializedName("1")
    @ExposeprivateList<String> _1 = null;
    @SerializedName("users")
    @ExposeprivateUsers users;
    private final static long serialVersionUID = 5880229946098431789L;

    /**
     * No args constructor for use in serialization
     * 
     */publicExample() {
    }

    /**
     * 
     * @paramusers
     * @param_0
     * @param_1
     */publicExample(String _0, List<String> _1, Users users) {
        super();
        this._0 = _0;
        this._1 = _1;
        this.users = users;
    }

    publicStringget0() {
        return _0;
    }

    publicvoidset0(String _0) {
        this._0 = _0;
    }

    publicExamplewith0(String _0) {
        this._0 = _0;
        returnthis;
    }

    publicList<String> get1() {
        return _1;
    }

    publicvoidset1(List<String> _1) {
        this._1 = _1;
    }

    publicExamplewith1(List<String> _1) {
        this._1 = _1;
        returnthis;
    }

    publicUsersgetUsers() {
        return users;
    }

    publicvoidsetUsers(Users users) {
        this.users = users;
    }

    publicExamplewithUsers(Users users) {
        this.users = users;
        returnthis;
    }

    @OverridepublicStringtoString() {
        returnnewToStringBuilder(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 = newGson().fromJson(/*put your json response variable here*/, Root.class);

and use root object to fetch/set any data via public get methods.

Post a Comment for "How To Parse Json Response Using Gson Library In Android?"