Vid
Vid

Reputation: 1012

Json string parsing using ObjectMapper?

I have this json string to parse {"uid":8,"totalPoints":"7740"} I have written the below class for this.

public class Points extends WebRequest implements IWebRequest {

    private static final String CLASS_TAG = Points.class.getSimpleName();
    private WebAPIResponse mWebAPIResponse;
    private int mUserId;

    /**
     * Initialize object
     * @param urlEndPoint
     * @param uiDelegate
     * @param appContext
     * @param webServiceRequestCallback
     */
    public Points(String urlEndPoint, IUIDelegate uiDelegate,
            WeakReference<Context> appContext,
            IWebServiceRequestCallback webServiceRequestCallback) {
        super(urlEndPoint, uiDelegate, appContext, webServiceRequestCallback);
    }

    @Override
    public String parseResponse(String responseString) {

        if (MBUtil.isEmpty(responseString)) {
            return "";
        }

        String errMsg = "";

        try {

            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);

            if (!objectMapper.canDeserialize(objectMapper.constructType(WebAPIResponse.class))) {
                return getAppContext().getString(R.string.msg_error_in_reading_format);
            }

            WebAPIResponse webAPIResponse = objectMapper.readValue(responseString, WebAPIResponse.class);
            this.mWebAPIResponse = webAPIResponse;

            Errors errors = webAPIResponse.getErrors();

            if (errors != null) {
                errMsg = errors.getMsg();
            }

        } catch (Exception e) {
            Log.e(CLASS_TAG, e.getMessage());
            errMsg = e.getMessage();
        }
        return errMsg;
    }

    @Override
    public JSONObject buildRequestBody() {

        JSONObject jsonObject = new JSONObject();
        Context context = getAppContext();
        if(context == null) {
            return jsonObject;
        }

        try {

            // Authentication body parameters
            JSONObject authenticationJsonObject = new JSONObject();
            authenticationJsonObject.put(context.getString(R.string.key_points_uid), mUserId);
            return authenticationJsonObject;

        } catch (Exception e) {
            Log.e(CLASS_TAG, e.getMessage());
        }
        return jsonObject;
    }

    public int getUserId() {
        return mUserId;
    }

    public void setUserId(int mUserId) {
        this.mUserId = mUserId;
    }

    public WebAPIResponse getWebAPIResponse() {
        return mWebAPIResponse;
    }

    public void setWebAPIResponse(WebAPIResponse mWebAPIResponse) {
        this.mWebAPIResponse = mWebAPIResponse;
    }

    public static class WebAPIResponse {

        private Data pointsData;
        private Errors errors;

        public Data getPointsData() {
            return pointsData;
        }

        public void setPointsData(Data pointsData) {
            this.pointsData = pointsData;
        }

        public Errors getErrors() {
            return errors;
        }

        public void setErrors(Errors errors) {
            this.errors = errors;
        }
    }

    public static class Data {

        @JsonProperty("uid")
        private int uid;
        @JsonProperty("totalPoints")
        private int totalPoints;

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }

        public int getTotalPoints() {
            return totalPoints;
        }

        public void setTotalPoints(int totalPoints) {
            this.totalPoints = totalPoints;
        }
    }
}

I getting proper response in parseResponse() method which is,

responseString = {"uid":8,"totalPoints":"7740"}

But in the same pasreResponse() method once it reached to this line

if (!objectMapper.canDeserialize(objectMapper.constructType(WebAPIResponse.class))) {
                return getAppContext().getString(R.string.msg_error_in_reading_format);
            }

            WebAPIResponse webAPIResponse = objectMapper.readValue(responseString, WebAPIResponse.class);

Not responding any thing and unable to parse the string. Please anyone check whether my parsing class is correct or not and why it is not parsing.

Upvotes: 1

Views: 515

Answers (1)

codeaholicguy
codeaholicguy

Reputation: 1691

With your responseString = {"uid":8,"totalPoints":"7740"} you just can deserialize it by Data object only.

Data data = objectMapper.readValue(responseString, Data.class);

If you want to deserialize your JSON String to WebAPIResponse object, your responseString must be:

{"pointsData":{"uid":8,"totalPoints":"7740"}, "errors": ...}

Upvotes: 2

Related Questions