-3

I am new in android development

please help me how to parse json data via GSON

{"response_code":0,"response_message":"User Exists","response_obj":{"email_exist":false,"number_exist":true}}

  GsonBuilder builder = new GsonBuilder();
                    builder.setPrettyPrinting();
                    Gson gson = builder.create();
                    IsUserResponse isUserResponse = gson.fromJson(response.body(), IsUserResponse.class);

when i try to fetch value

 Log.d("response code", isUserResponse.getResponse_code() + "" + isUserResponse.isNumber_exist());

then i am not getting value of

isUserResponse.isNumber_exist();

please help me how to get value of isnumberexist

IsUserResponse.java

public class IsUserResponse extends BaseResponse {

    private boolean email_exist;
    private boolean number_exist;

    public boolean isEmail_exist() {
        return email_exist;
    }

    public void setEmail_exist(boolean email_exist) {
        this.email_exist = email_exist;
    }

    public boolean isNumber_exist() {
        return number_exist;
    }

    public void setNumber_exist(boolean number_exist) {
        this.number_exist = number_exist;
    }


}

BaseResponse.java

public class BaseResponse {
    private int response_code;
    private String response_message;


    public int getResponse_code() {
        return response_code;
    }

    public void setResponse_code(int response_code) {
        this.response_code = response_code;
    }

    public String getResponse_message() {
        return response_message;
    }

    public void setResponse_message(String response_message) {
        this.response_message = response_message;
    }
}
Ajit Kumar
  • 11
  • 2

2 Answers2

1

try this add this gradle in your build.gradle compile 'com.google.code.gson:gson:2.7' and create pojo below like

    import java.util.List;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Example {

        @SerializedName("response_code")
        @Expose
        private Integer responseCode;
        @SerializedName("response_message")
        @Expose
        private String responseMessage;
        @SerializedName("response_obj")
        @Expose
        private ResponseObj responseObj;

        public Integer getResponseCode() {
            return responseCode;
        }

        public void setResponseCode(Integer responseCode) {
            this.responseCode = responseCode;
        }

        public String getResponseMessage() {
            return responseMessage;
        }

        public void setResponseMessage(String responseMessage) {
            this.responseMessage = responseMessage;
        }

        public ResponseObj getResponseObj() {
            return responseObj;
        }

        public void setResponseObj(ResponseObj responseObj) {
            this.responseObj = responseObj;
        }
        public class ResponseObj {

            @SerializedName("email_exist")
            @Expose
            private Boolean emailExist;
            @SerializedName("number_exist")
            @Expose
            private Boolean numberExist;

            public Boolean getEmailExist() {
                return emailExist;
            }

            public void setEmailExist(Boolean emailExist) {
                this.emailExist = emailExist;
            }

            public Boolean getNumberExist() {
                return numberExist;
            }

            public void setNumberExist(Boolean numberExist) {
                this.numberExist = numberExist;
            }

        }
}

then

Example example = new Gson().fromJson("put your json above response",Example.class);
example.getResponseObj().getNumberExist();

here reference link for creating POJO and in the url select option Source type:JSON and Annotation style:GSON

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
0

1. Add GSON dependency

compile 'com.google.code.gson:gson:2.6.2'

2. Add following classes MyResponse and MyResponseOb to your project

3. Parse Json using

Gson gson = new Gson();
MyResponse response = gson.fromJson(jsonLine, MyResponse.class);

MyResponse.java

import com.google.gson.annotations.SerializedName;

public class MyResponse {

    @SerializedName("response_code")
    int responseCode;

    @SerializedName("response_message")
    String responseMessage; 

    @SerializedName("response_obj")
    MyResponseOb responseObject;    

}

MyResponseOb.java

import com.google.gson.annotations.SerializedName;

public class MyResponseOb {

    @SerializedName("email_exist")
    bool emailExists;

    @SerializedName("number_exist")
    bool numberExists;  

}
Community
  • 1
  • 1
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53