Isuru
Isuru

Reputation: 3958

Type JSONObject cannot be converted to JSONArray in Volley

I'm trying to read the registration numbers from JSON feed.

{"error":false,"0":[{"registration_number":"HJ-1756"},{"registration_number":"ABD-1234"}]}

But I have hard time doing it with following code.

JsonArrayRequest req = new JsonArrayRequest(Constants.URL_VEHICLE_REG_JSON,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            jsonResponse = "";

                            // get an array from the JSON object
                            JSONArray res = (JSONArray) response.get(1);

                            for (int i = 0; i < res.length(); i++) {

                                //JSONObject car = (JSONObject) response.get(i);
                                JSONObject innerObj = (JSONObject)res.get(i);

                                String registrationNumber = innerObj.getString("registration_number");

                                jsonResponse += "registration_number: " + registrationNumber + "\n\n";

                                Log.d(TAG, jsonResponse);

                                items.add(registrationNumber);
                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                        adapter.notifyDataSetChanged();

                        hideDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Error: " + error.getMessage());
                hideDialog();
            }
        });

I tried changing Listener to Listener and response as well. But it didn't work. I understand that I get JSONObject as a result. But where do I gone wrong?

Upvotes: 0

Views: 1082

Answers (2)

Binesh Kumar
Binesh Kumar

Reputation: 2860

You try this code

            JSONArray jsonArray = jsonObject.getJSONArray("0");
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                        String regNum = jsonObj.getString("registration_number");
                    }

Upvotes: 0

BNK
BNK

Reputation: 24114

Firstly, because your JSON response is a JSONObject, then you should use Volley's JsonObjectRequest instead of JsonArrayRequest.

Then, let's assume that you successfully get JSONObject response data, you can use the following sample code:

            if (jsonObject != null && !jsonObject.isNull("0")) {
                JSONArray jsonArray = jsonObject.getJSONArray("0");
                if (jsonArray != null && jsonArray.length() > 0) {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj = jsonArray.getJSONObject(i);
                        if (jsonObj != null && !jsonObj.isNull("registration_number")){
                            String regNum = jsonObj.getString("registration_number");
                        }
                    }
                }
            }

Hope it helps!

Upvotes: 2

Related Questions