通过GSON android解析json数据

时间:2017-05-11 07:09:48

标签: android gson

我是android开发的新手

请帮我解决如何通过GSON解析json数据

{"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);

当我尝试获取值

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

然后我没有得到

的价值
isUserResponse.isNumber_exist();

请帮助我如何获得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;
    }
}

2 个答案:

答案 0 :(得分:1)

尝试在build.gradle compile 'com.google.code.gson:gson:2.7'中添加此gradle并在下面创建pojo

    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;
            }

        }
}

然后

Example example = new Gson().fromJson("put your json above response",Example.class);
example.getResponseObj().getNumberExist();
用于创建POJO的

here参考链接以及网址选择选项Source type:JSONAnnotation style:GSON

答案 1 :(得分:0)

1。添加GSON依赖

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

2。将以下类MyResponseMyResponseOb添加到您的项目中

3. 使用

解析Json
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;  

}