Deserialize Json

Asked

Viewed 78 times

0

How can I deserialize this json? I am learning now communication with web service and I have arisen this doubt, I managed to get the return of web service follows my code:

private void makeJsonObjReq(){
    showProgressDialog();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            Const.URL_JSON_OBJECT, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                    hideProgressDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {

        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("id", "id");
            params.put("sigla", "sigla");

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq,
            tag_json_obj);


    // Cancelling request
    // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);       
}

return of the Json

{"sigla":"AM","id":2}

I would like some tip like to take only AM and 2. if you have some other simpler way to make that connection.

1 answer

1


I managed to solve, I do not know if it is the best practice more this returning me what I need.

private void makeJsonObjReq(){
        showProgressDialog();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                Const.URL_JSON_OBJECT, null,
                new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                    hideProgressDialog();
                    JSONObject jObject = null;
                    int aJsonint = 0;
                    String aJsonString;
                    try {
                        jObject = new JSONObject(String.valueOf(response));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    try {
                        aJsonString = jObject.getString("sigla");
                        aJsonint = jObject.getInt("id");
                        System.out.println("ID " + aJsonint + " sigla  " + aJsonString);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {


    };

    AppController.getInstance().addToRequestQueue(jsonObjReq,
            tag_json_obj);

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.