简体   繁体   中英

Android Jackson Library parsing

I am getting a "JSONObject can't be cast to ClassResponse" error after getting a server response. Im guessing its because the response from server doesn't match the fields in ClassResponse? Is it because of the "data" struct in the JSON response? I have a setter for each of the fields but not sure what I am doing wrong. Is there a way to change the annotation for the Jackson library to parse other layers of the json object? for instance..

@JsonProperty("data:accessToken") or something of that nature... or is that even necessary?

server response:

{
    success: "true"
    message: "Record Created"
    data: {
      userToken : "1"
          }
}

ClassResponse:

@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginResponse extends Response<LoginResponse.Result>
{
    public static class Result
    {

        private String mUserToken;
        private String mAccessToken;
        private String mUserId;

        public String getUserToken(){
            return mUserToken;
        }

        @JsonProperty("userToken")
        public void setUserToken(final String aUserToken){
            mUserToken = aUserToken;
        }

        public String getAccessToken(){
            return mAccessToken;
        }

        @JsonProperty("accessToken")
        public void setAccessToken(final String aAccessToken){
            mAccessToken = aAccessToken;
        }

        public String getUserId(){
            return mUserId;
        }

        @JsonProperty("user_id")
        public void setUserId(final String aUserId){
            mUserId = aUserId;
        }
    }
}

    (edit)
    @JsonProperty("data")
    public void accessDataLayer(final String data) throws JSONException {

        JSONObject jobj = new JSONObject(data);

        mUserToken = jobj.getString("userToken");
        mAccessToken = jobj.getString("accessToken");
        mUserId = jobj.getString("user_id");

    }



@Override
public RESULT loadDataFromNetwork() throws Exception{
    final String url = buildUrl();

    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    String response;

    try{
        conn.setDoInput(true);
        conn.setDoOutput(true);

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        JSONObject submitInfo = new JSONObject();
        submitInfo = buildParams(entity);

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(submitInfo.toString());
        out.close();

            response = readStream(conn.getErrorStream());

        conn.disconnect();
    }
    return parse(response);

Cause you are trying a desserialize a whole JSONObject in your "LoginResponse.class" collection. That's no ok, because LoginResponse is an element of the Array "data" in your JSON response, but NOT whole response! To get the array you should write something like this

 JsonArray data = (JsonArray) JSONObject.getAsJsonArray("data")

And after that you'll get your "data" collection

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