Gson反序列化应为BEGIN_ARRAY,但应为STRING [英] Gson deserialization Expected BEGIN_ARRAY but was STRING

查看:121
本文介绍了Gson反序列化应为BEGIN_ARRAY,但应为STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get the following json response from my api in my android application, i am using Gson deserialization to parse my data and populate my 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请求服务,省略了其他代码:

Api Request service, omitted the other code:

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

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

Request api call in my controller class, omitted the other code.

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列的路径$ .data

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 92 path $.data

推荐答案

数据参数值以"开头,因此它是字符串而不是数组.

data parameter value is starting with " So it is a String not an array.

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

尝试这个模型课:

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

}

这篇关于Gson反序列化应为BEGIN_ARRAY,但应为STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆