Manikanta
Manikanta

Reputation: 3421

Parsing JSON object with JSON array inside it using Volley

I have a Json reponse like this:

{
    "status" : "CREATED",
    "message" : "user created",
    "results" : [
        {
            "id" : "1243233",
        }
    ]
}

The above JSON response will be obtained with a POST call.In android using volley library i did a REST call as following:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                                url, null,
                                new Response.Listener<JSONObject>() {

                                    @Override
                                    public void onResponse(JSONObject response) {
                                        Log.d("Request", response.toString());
                                        try {
                                            response.getString("status");
                                            JSONArray array = response.getJSONArray("results");

                                                JSONObject id = (JSONObject)array.get(0);




                                            Toast.makeText(getApplicationContext(),response.getString("status"),Toast.LENGTH_LONG).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        pDialog.hide();

                                    }
                                }, new Response.ErrorListener() {

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

                                pDialog.hide();
                            }
                        }){
                            @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() throws AuthFailureError {
                                Map<String, String> params = new HashMap<String, String>();
                                params.put("phone", phone);
                                params.put("name", username);
                                params.put("pwd",password);
                                params.put("email", email);
                                return params;
                            }
                        };

                        AppController.getInstance().addToRequestQueue(jsonObjReq);

I am getting an error org.json.JSONException: Expected literal value at character 102 of

 {
    "status" : "CREATED",
    "message" : "user created",
    "results" : [
        {
            "id" : "1243233",
        }
    ]
}

Please help me to solve this.

Upvotes: 0

Views: 2689

Answers (1)

Arun Shankar
Arun Shankar

Reputation: 2295

There is a comma in the results array's item. Which means there should be another element(string,integer etc) as part of that JSONObject(first item of results array).

Hence you get the JSON parsing error.

    "results" : [
                   {
                     "id" : "1243233",
                    }
                ]

should be

   "results" : [
                 {
                      "id" : "1243233"
                 }
               ]

Removing comma

Upvotes: 1

Related Questions