简体   繁体   中英

GSON: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

While working on retrofit, I used http://www.jsonschema2pojo.org this site to convert json to POJO. But I got an error while parsing JSON like this. Its saying Expected BEGIN_OBJECT but was BEGIN_ARRAY .

[
  {
    "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
  }
]

Here is my modal class used for json to 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;
    }
}

Here I am calling the method to get the response.

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

And here is the log details

D/OkHttp: [{"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}]


D/TAG: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Can anyone help me out? How to solve this error?

The response that you are getting from the server is a list of UserAboutModel . However, in your code, you are expecting a single data. I think the function should look like the following.

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
}

Instead of Response<UserAboutModel> use a Response<List<UserAboutModel>> so that it tells the function to expect a list of UserAboutModel .

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM