简体   繁体   English

使用ObjectMapper解析JSON字符串?

[英]Json string parsing using ObjectMapper?

I have this json string to parse {"uid":8,"totalPoints":"7740"} I have written the below class for this. 我有这个json字符串来解析{"uid":8,"totalPoints":"7740"}我为此编写了下面的类。

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, 我在parseResponse()方法中得到了正确的响应,即

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

But in the same pasreResponse() method once it reached to this line 但是一旦到达此行,就使用相同的pasreResponse()方法

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. 请任何人检查我的解析类是否正确以及为什么不解析。

With your responseString = {"uid":8,"totalPoints":"7740"} you just can deserialize it by Data object only. 使用responseString = {"uid":8,"totalPoints":"7740"}您只能通过Data对象反序列化它。

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

If you want to deserialize your JSON String to WebAPIResponse object, your responseString must be: 如果要将JSON字符串反序列化为WebAPIResponse对象,则responseString必须为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM