0

I'm working with retrofit2 In standard json response : {url}/login

{
  "message": "succesfully",
  "status_code": "100",
  "data":{
    "token":"jiur02Pje9",
    "username":"usertest",
    "id":"jlow81"
  }
}

and another api:

{
  "message": "get information succesfully",
  "status_code": 100,
  "data": {
    "username": "usertest02",
    "phone": "1111111",
    "image": "http://test/1.jpg",
    "online_status": 1
  }
}

I create file model

BaseData.java

String message;
int statusCode;
Object data;

UserInformation.java (field : username, image ...)

Authentication.java (field : token, id, username ...)

How to parse [data] to UserInformation and Authentication object

Thank for reading.

Cùi Bắp
  • 68
  • 1
  • 2
  • 9

1 Answers1

0

There are many ways of doing it.

You can go ahead with third party libraries like Gson

Your pojo class, will look somewhat like this, use Android plugin or online json to pojo convertor.

public class Response {

    @SerializedName("data")
    private Data mData;
    @SerializedName("message")
    private String mMessage;
    @SerializedName("status_code")
    private String mStatusCode;

    public Data getData() {
        return mData;
    }

    public void setData(Data data) {
        mData = data;
    }

    public String getMessage() {
        return mMessage;
    }

    public void setMessage(String message) {
        mMessage = message;
    }

    public String getStatusCode() {
        return mStatusCode;
    }

    public void setStatusCode(String statusCode) {
        mStatusCode = statusCode;
    }

    public class Data {

        @SerializedName("image")
        private String mImage;
        @SerializedName("online_status")
        private Long mOnlineStatus;
        @SerializedName("phone")
        private String mPhone;
        @SerializedName("username")
        private String mUsername;
        @SerializedName("token")
        private String mToken;
        @SerializedName("id")
        private String mId;
        public String getImage() {
            return mImage;
        }

        public void setImage(String image) {
            mImage = image;
        }

        public Long getOnlineStatus() {
            return mOnlineStatus;
        }

        public void setOnlineStatus(Long onlineStatus) {
            mOnlineStatus = onlineStatus;
        }

        public String getPhone() {
            return mPhone;
        }

        public void setPhone(String phone) {
            mPhone = phone;
        }

        public String getUsername() {
            return mUsername;
        }

        public void setUsername(String username) {
            mUsername = username;
        }

        public String getmToken() {
            return mToken;
        }

        public void setmToken(String mToken) {
            this.mToken = mToken;
        }

        public String getmId() {
            return mId;
        }

        public void setmId(String mId) {
            this.mId = mId;
        }
    }

}

Now to convert json string to pojo, use Gson as -

Gson gson = new Gson();
Response response = gson.fromJson(yourresponseStr.toString(), Response.class);

You can use the same response class, or you can have mapper class which will take Response as param and returns different pojo's by mapping the state with UserInformation.java or Authentication.java

This way code is more readable and cleaner and you are using only the data you need. Also, if you need to serialize and pass to other components, you will be passing only the required fields.

Ritt
  • 3,181
  • 3
  • 22
  • 51
  • Thanks your reply. But I think it not clear and hard maintain. Because my project have 20+ API for JSON standard like above. – Cùi Bắp Jul 04 '17 at 07:45
  • you need to have a pojo class for each api, to bind to. Have a base class for common. – Ritt Jul 04 '17 at 08:33