我正在使用gson进行改造来解析json响应但是我得到了上面提到的异常。 我曾尝试在网上搜索并自己搞清楚,但到目前为止还没有取得任何成功
这是我的json回复:
{
"success": "true",
"message": "",
"data": [{
"Id": "56B5D1408B8D9",
"Name": "John",
"Age":"28",
"place": "Central City"
}]
}
my Pojo Class
public class Datum {
@SerializedName("Id")
@Expose
private String Id;
@SerializedName("Name")
@Expose
private String Name;
@SerializedName("Age")
@Expose
private String Age;
@SerializedName("place")
@Expose
private String place;
/**
*
* @return
* The Id
*/
public String getId() {
return Id;
}
/**
*
* @param Id
* The Id
*/
public void setId(String Id) {
this.Id = Id;
}
/**
*
* @return
* The Name
*/
public String getName() {
return Name;
}
/**
*
* @param Name
* The Name
*/
public void setName(String Name) {
this.Name = Name;
}
/**
*
* @return
* The Age
*/
public String getAge() {
return Age;
}
/**
*
* @param Age
* The Age
*/
public void setAge(String Age) {
this.Age = Age;
}
/**
*
* @return
* The place
*/
public String getPlace() {
return place;
}
/**
*
* @param place
* The place
*/
public void setPlace(String place) {
this.place = place;
}
}
父类 package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class UserModel {
@SerializedName("success")
@Expose
private String success;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Datum> data = new ArrayList<Datum>();
/**
*
* @return
* The success
*/
public String getSuccess() {
return success;
}
/**
*
* @param success
* The success
*/
public void setSuccess(String success) {
this.success = success;
}
/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* The data
*/
public List<Datum> getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(List<Datum> data) {
this.data = data;
}
}
这是我解析它的代码
private void getUserDetail(){
Call<UserModel> userModelCall = Util.getEndPointService().fetchUser(sessionId, startNo);
userModelCall.enqueue(new Callback<UserModel>() {
@Override
public void onResponse(Response<UserModel> response) {
int resCode = response.code();
Log.i(TAG_DEBUG, "response code is " + resCode);
if (resCode == 200) {
UserModel userModel = response.body();
Log.i(TAG_DEBUG, userModel.getSuccess());
if (userModel.getSuccess().equals(StringConstants.TRUE)) {
retrieveDownloadedProfile(userModel);
}
}
}
@Override
public void onFailure(Throwable t) {
CLog.e(TAG_DEBUG, "Exception in getUserDetail " + t);
t.printStackTrace();
}
});
}
答案 0 :(得分:0)
Gson将从根开始工作,您需要一个与您的回复相符的父母,如:
class MyResponse {
public List<Datum> data;
}
您的Datum类看起来与响应
中data
数组中的项匹配