Java GSON:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$
在进行改装时,我使用了Java GSON:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$,java,android,json,retrofit,jsonexception,Java,Android,Json,Retrofit,Jsonexception,在进行改装时,我使用了http://www.jsonschema2pojo.org此站点用于将json转换为POJO。但是我在像这样解析JSON时出错了。它说应该是BEGIN\u对象,但却是BEGIN\u数组 [ { "uuid": "12e26270-b506-11e9-ad81-5f542bb63d66", "first_name": "Nohar", "last_name": "Kumar", "title": "Premier League Playe
http://www.jsonschema2pojo.org
此站点用于将json转换为POJO。但是我在像这样解析JSON时出错了。它说应该是BEGIN\u对象,但却是BEGIN\u数组
[
{
"uuid": "12e26270-b506-11e9-ad81-5f542bb63d66",
"first_name": "Nohar",
"last_name": "Kumar",
"title": "Premier League Player",
"gender": "N/A",
"date_of_birth": null,
"relationship_status": null,
"fav_quote": null,
"best_achievement": null,
"experience": null,
"skills": null,
"height": null,
"weight": null,
"about": null
}
]
下面是我用于json到POJO的模式类
public class UserAboutModel {
@SerializedName("uuid")
@Expose
private String uuid;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("title")
@Expose
private String title;
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("date_of_birth")
@Expose
private String dateOfBirth;
@SerializedName("relationship_status")
@Expose
private String relationshipStatus;
@SerializedName("fav_quote")
@Expose
private String favQuote;
@SerializedName("best_achievement")
@Expose
private String bestAchievement;
@SerializedName("experience")
@Expose
private String experience;
@SerializedName("skills")
@Expose
private String skills;
@SerializedName("about")
@Expose
private String about;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getRelationshipStatus() {
return relationshipStatus;
}
public void setRelationshipStatus(String relationshipStatus) {
this.relationshipStatus = relationshipStatus;
}
public String getFavQuote() {
return favQuote;
}
public void setFavQuote(String favQuote) {
this.favQuote = favQuote;
}
public String getBestAchievement() {
return bestAchievement;
}
public void setBestAchievement(String bestAchievement) {
this.bestAchievement = bestAchievement;
}
public String getExperience() {
return experience;
}
public void setExperience(String experience) {
this.experience = experience;
}
public String getSkills() {
return skills;
}
public void setSkills(String skills) {
this.skills = skills;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
}
在这里,我调用该方法以获得响应
private void getUserAbout() {
apiInterface = APIClient.getClient().create(ApiInterface.class);
SharedPreferences sp = getSharedPreferences("UserData", Context.MODE_PRIVATE);
String token = sp.getString("User_Token", "");
Log.v("working", "working");
Call<UserAboutModel> call = apiInterface.userAboutBasic(currentUserUuid, "Bearer " + token);
call.enqueue(new Callback<UserAboutModel>() {
@Override
public void onResponse(Call<UserAboutModel> call, Response<UserAboutModel> response) {
if (response.isSuccessful()) {
String name = response.body().getFirstName() + " " + response.body().getLastName();
SharedPreferences pref = getSharedPreferences("UserAbout", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
Log.v("NAme", name);
Log.v("Title", response.body().getTitle());
editor.putString("UserName", name);
editor.putString("UserTitle", response.body().getTitle());
editor.putString("UserDOB", response.body().getDateOfBirth());
editor.putString("UserFAvQuote", response.body().getFavQuote());
editor.putString("UserSkill", response.body().getSkills());
editor.putString("UserGender", response.body().getGender());
editor.putString("UserRelationshipStatus", response.body().getRelationshipStatus());
editor.putString("UserExperience", response.body().getExperience());
editor.putString("UserBestAchievment", response.body().getBestAchievement());
editor.putString("UserCategory", primarySports.getText().toString());
editor.putString("UserSports", response.body().getExperience());
editor.apply();
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getApplicationContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<UserAboutModel> call, Throwable t) {
call.cancel();
Log.d("TAG", t.toString());
}
});
}
有人能帮我吗?如何解决此错误?您从服务器得到的响应是
用户aboutmodel的列表。但是,在代码中,您需要一个数据。我认为函数应该如下所示
public void onResponse(Call<UserAboutModel> call, Response<List<UserAboutModel>> response) {
// Now take the first element from the response list
// and then do the rest of your work
}
public void onResponse(调用、响应){
//现在从响应列表中获取第一个元素
//然后完成剩下的工作
}
不要使用Response
而使用Response
,这样它会告诉函数需要一个UserAboutModel
列表
希望有帮助
public void onResponse(Call<UserAboutModel> call, Response<List<UserAboutModel>> response) {
// Now take the first element from the response list
// and then do the rest of your work
}