通过GSON-android解析json数据
我是android开发的新手 请帮助我如何通过GSON解析json数据通过GSON-android解析json数据,android,gson,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();
{"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();
请帮我如何获得ISNumberList的值
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;
}
}
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;
}
}
BaseResponse.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;
}
}
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;
}
}
尝试在你的
build.gradle
编译'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;
}
}
}
然后
用于创建POJO的参考链接,并在url中选择选项源类型:JSON
和注释样式:GSON
1。添加依赖项
compile 'com.google.code.gson:gson:2.6.2'
2.将以下类添加到项目中MyResponse
和MyResponseOb
3.使用
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;
}
import com.google.gson.annotations.SerializedName;
public class MyResponseOb {
@SerializedName("email_exist")
bool emailExists;
@SerializedName("number_exist")
bool numberExists;
}
MyResponseOb.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;
}
import com.google.gson.annotations.SerializedName;
public class MyResponseOb {
@SerializedName("email_exist")
bool emailExists;
@SerializedName("number_exist")
bool numberExists;
}
你挖过谷歌吗?是的,但还不清楚它的概念,它是如何工作的。有很多文档,其中很多都清楚,可能有足够多的副本