Gson反序列化预计BEGIN_ARRAY但是STRING

时间:2018-03-09 07:03:23

标签: java android gson

我在我的Android应用程序中从我的api获得以下json响应,我正在使用Gson反序列化来解析我的数据并填充我的listview。

API响应:

{  
   "message":"success",
   "success":"1",
   "sessionKey":"a422e60213322845b85ae122de53269f",
   "data":"[{\"system_id\":1,\"logo_url\":\"https:\\\/\\\/www.beta.system.com\\\/api\\\/icons\\\/3244ffjg.jpg\",\"organization_name\":\"Test Organasation\"}]"
}

我的班级:

    public class SystemResponse {

    @Expose
    @SerializedName("message")
    private String message;

    @Expose
    @SerializedName("success")
    private String statusCode;

    @Expose
    @SerializedName("sessionKey")
    private String sessionKey;

    @Expose
    @SerializedName("data")
    private List<System> data;

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<System> getSystems() {
        return data;
    }

    public void setSystems(List<System> data) {
        this.data = data;
    }

    public static class System{
        @Expose
        @SerializedName("system_id")
        private Long systemId;

        @Expose
        @SerializedName("logo_url")
        private String logoUrl;

        @Expose
        @SerializedName("organization_name")
        private String organizationName;

        public Long getSystemId() {
            return systemId;
        }

        public void setSystemId(Long systemId) {
            this.systemId = systemId;
        }

        public String getLogoUrl() {
            return logoUrl;
        }

        public void setLogoUrl(String logoUrl) {
            this.logoUrl = logoUrl;
        }

        public String getOrganizationName() {
            return organizationName;
        }

        public void setOrganizationName(String organizationName) {
            this.organizationName = organizationName;
        }
    }
}

Api请求服务,省略了其他代码:

public Observable<SystemResponse> doServerAccountRequestApiCall(String param) {
        return   Rx2AndroidNetworking.get(ApiEndPoint.ENDPOINT_GET_ACCOUNTS)
                .build()
                .getObjectObservable(SystemResponse.class);
    }

在我的控制器类中请求api调用,省略了其他代码。

getCompositeDisposable().add(getDataManager()          .doServerAccountRequestApiCall(params))
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(new Consumer<SystemResponse>() {
                    @Override
                    public void accept(@NonNull SystemResponse response)
                            throws Exception {
                        //error here

                    }

                }, new Consumer<Throwable>() {}));

我收到了这个错误:

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第92列为STRING路径$ .data

1 个答案:

答案 0 :(得分:5)

  

数据参数值以"开头所以它是一个字符串而不是一个数组。

使用http://www.jsonschema2pojo.org/生成模型类。

enter image description here

试试这个模特课:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("message")
@Expose
private String message;
@SerializedName("success")
@Expose
private String success;
@SerializedName("sessionKey")
@Expose
private String sessionKey;
@SerializedName("data")
@Expose
private String data;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getSuccess() {
return success;
}

public void setSuccess(String success) {
this.success = success;
}

public String getSessionKey() {
return sessionKey;
}

public void setSessionKey(String sessionKey) {
this.sessionKey = sessionKey;
}

public String getData() {
return data;
}

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

}