预计BEGIN_OBJECT但行1列128是STRINGBEGIN_OBJECT、STRING

2023-09-06 08:38:01 作者:他似人间晚风

我有使用GSON库我的code的一个问题。

当我跑我得到以下错误:

  5596-5596 / be.appmax.ktsjjt E / AndroidRuntime:致命异常:主要com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但STRING在第1行128列 

有没有一种方法,以获取有关错误的更多详细?

苹果与星巴克的同种焦虑

这是我的JSON,我得到:

{\"data\":{\"pro_email\":\"kevingoos@telenet.be\",\"pro_last_name\":\"Goos\",\"pro_sex\":\"M\",\"pro_nickname\":\"Kevin Goos\",\"pro_date_birth\":\"1996-04-23\",\"pro_id\":14,\"pro_facebook_id\":\"1333567985\",\"pro_first_name\":\"Kevin\",\"pro_avatar\":\"http:\\/\\/graph.facebook.com\\/1333567985\\/picture\",\"pro_date_updated\":\"2014-04-09十一时32分20秒},状态:200}

这是解析JSON类:

Response类:

 公共类ProfileResponse {@SerializedName(身份)私人诠释的地位;@SerializedName(数据)私人档案数据;公共ProfileResponse(){}公众诠释的getStatus(){    返回状态;}公共无效setStatus(INT状态){    this.status =状态;}公开信息的getData(){    返回的数据;}公共无效使用setData(配置文件数据){    this.data =数据;}} 

该配置文件类:

 公共类档案实现Parcelable {//ID@SerializedName(pro_id)私人诠释身份证;// FIELDS@SerializedName(pro_nickname)私人字符串的绰号;@SerializedName(pro_email)私人字符串电子邮件;@SerializedName(pro_sex)私人字符串性;//时间或日期@SerializedName(pro_date_birth)私人的DateTime birth_day;@SerializedName(pro_date_updated)私人的DateTime更新;@SerializedName(pro_avatar)私人字符串化身;@SerializedName(pro_first_name)私有String的firstName;@SerializedName(pro_last_name)私人字符串的lastName;@SerializedName(pro_facebook_id)私人字符串facebookId;公开资料(){}公众诠释的getId(){    返回ID;}公共无效SETID(INT ID){    this.id = ID;}公共字符串getNickname(){    返回的绰号;}公共无效setNickname(字符串昵称){    this.nickname =昵称;}公共字符串getEmail(){    返回的电子邮件;}公共无效setEmail(字符串email){    this.email =电子邮件;}公共字符串getSex(){    返回性别;}公共无效setSex(字符串性别){    this.sex =性;}公众的DateTime getBirth_day(){    返回birth_day;}公共无效setBirth_day(字符串日期){    this.birth_day = JodaTimeUtil.getDateTimeFromString(日期);}公众的DateTime getUpdated(){    返回更新;}公共无效setUpdated(字符串日期){    this.updated = JodaTimeUtil.getDateTimeFromString(日期);}公共字符串getAvatar(){    返回化身;}公共无效setAvatar(字符串头像){    this.avatar =化身;}公共字符串的getFirstName(){    返回的firstName;}公共无效setFirstName(字符串的firstName){    this.firstName =名字;}公共字符串getLastName(){    返回lastName的;}公共无效setLastName(字符串的lastName){    this.lastName = lastName的;}公共字符串getFacebookId(){    返回facebookId;}公共无效setFacebookId(字符串facebookId){    this.facebookId = facebookId;}@覆盖公共字符串的toString(){    返回ID:+身份证+ -  FBID:+ facebookId + - 产品名称:+昵称;}@覆盖公众诠释describeContents(){    返回0;}} 

这捕获响应响应CLASSE:

 公共静态档案getProfile(JSONObject的响应)抛出IOException    个人资料个人资料= NULL;    //地图JSON Java对象    键入ProfileResponse =新TypeToken< ObjectResponse<串GT;>(){}的getType()。    ProfileResponse profileResponse = Shared.gson.fromJson(response.toString(),ProfileResponse.class);    如果(profileResponse!= NULL){        //状态,信息,数据        INT状态= profileResponse.getStatus();        Tools.LOG_DEBUG(ResponseBeers  -  GETALL,状态:+状态);        如果(profileResponse.getData()!= NULL){            简介= profileResponse.getData();        }    }    返回的个人资料;} 

解决方案

找到我的问题:他不知道从乔达,日期时间库DateTime类型。所以不得不注册该类型TypeAdapter。

  GSON GSON =新GsonBuilder()registerTypeAdapter(DateTime.class,新JsonDeserializer<&日期时间GT;(){@覆盖公众的DateTime反序列化(JsonElement JSON,类型typeOfT,JsonDeserializationContext上下文)抛出JsonParseException {       返回JodaTimeUtil.getDateTimeFromString(json.getAsString());}})。创建(); 

I have a problem with the my code using the Gson library.

When I run I get the following error:

5596-5596/be.appmax.ktsjjt E/AndroidRuntime﹕ FATAL EXCEPTION: main
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 128

Is there a way to get more detail about the error?

This is my json that I get:

{"data":{"pro_email":"kevingoos@telenet.be","pro_last_name":"Goos","pro_sex":"M","pro_nickname":"Kevin Goos","pro_date_birth":"1996-04-23","pro_id":14,"pro_facebook_id":"1333567985","pro_first_name":"Kevin","pro_avatar":"http:\/\/graph.facebook.com\/1333567985\/picture","pro_date_updated":"2014-04-09 11:32:20"},"status":200}

And this are the classes that parse the json:

The Response class:

public class ProfileResponse {

@SerializedName("status")
private int status;
@SerializedName("data")
private Profile data;

public ProfileResponse() {
}

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public Profile getData() {
    return data;
}

public void setData(Profile data) {
    this.data = data;
}
}

The profile class:

public class Profile implements Parcelable {

//ID
@SerializedName("pro_id")
private int id;
//FIELDS
@SerializedName("pro_nickname")
private String nickname;
@SerializedName("pro_email")
private String email;
@SerializedName("pro_sex")
private String sex;
//TIME OR DATE
@SerializedName("pro_date_birth")
private DateTime birth_day;
@SerializedName("pro_date_updated")
private DateTime updated;
@SerializedName("pro_avatar")
private String avatar;
@SerializedName("pro_first_name")
private String firstName;
@SerializedName("pro_last_name")
private String lastName;
@SerializedName("pro_facebook_id")
private String facebookId;

public Profile() {

}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public DateTime getBirth_day() {
    return birth_day;
}

public void setBirth_day(String date) {
    this.birth_day = JodaTimeUtil.getDateTimeFromString(date);
}

public DateTime getUpdated() {
    return updated;
}

public void setUpdated(String date) {
    this.updated = JodaTimeUtil.getDateTimeFromString(date);
}

public String getAvatar() {
    return avatar;
}

public void setAvatar(String avatar) {
    this.avatar = avatar;
}

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 getFacebookId() {
    return facebookId;
}

public void setFacebookId(String facebookId) {
    this.facebookId = facebookId;
}

@Override
public String toString() {
    return "Id: " + id + " - FBID: " + facebookId + " - NAME: " + nickname;
}

@Override
public int describeContents() {
    return 0;
}
}

Response classe that catches the response:

public static Profile getProfile(JSONObject response) throws IOException {
    Profile profile = null;

    // Map JSON to JAVA Objects
    Type ProfileResponse = new TypeToken<ObjectResponse<String>>(){}.getType();
    ProfileResponse profileResponse = Shared.gson.fromJson(response.toString(), ProfileResponse.class);

    if (profileResponse != null) {
        // Status, Message, Data
        int status = profileResponse.getStatus();

        Tools.LOG_DEBUG("ResponseBeers - getAll, Status: " + status);

        if (profileResponse.getData() != null) {
            profile = profileResponse.getData();
        }
    }
    return profile;
}

解决方案

Found my problem: He didn't know the DateTime type from Joda-Datetime library. So had to register a TypeAdapter for that type.

Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
@Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
       return JodaTimeUtil.getDateTimeFromString(json.getAsString());
}
}).create();