Deserialize Json

0

How can I deserialize this json ?? I'm learning now communication with web service and I've had this doubt, I got the return of the 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 from Json

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

I would like some tips on how to get only AM and 2. if you have any other simpler way to make that connection.

    
asked by anonymous 17.09.2014 / 16:18

1 answer

1

I was able to solve, I do not know if it is the best practice, and this is 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);

}
    
17.09.2014 / 16:31