2

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 Response:

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

My Class:

    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 Request service, omitted the other code:

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

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

I am getting getting this error:

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

Innocent
  • 41
  • 8

1 Answers1

5

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

Use http://www.jsonschema2pojo.org/ to generate a model class.

enter image description here

Try this model class :

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

}
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
  • 1
    Just referring to http://www.jsonschema2pojo.org was worth an upvote. (was not aware it existed) Helped me with my problem as well! – thomas77 Mar 09 '18 at 07:22
  • 1
    @Innocent happy to help :) just check with you backend developer and get it right from service if you want array or object :P – Vidhi Dave Mar 09 '18 at 07:34