cnbandicoot
cnbandicoot

Reputation: 370

Get JSONArray with Volley in Android

I'm trying to get this JSONArray with android but I can't get codigo and nombre of all items.

I leave the JSON that I'm trying to get:

{"categorias": {"cfcd208495d565ef66e7dff9f98764da":{"orden":0,"codigo":"001","nombre":"TUBO Y ACCESORIO DE COBRE, LATON"}, "c4ca4238a0b923820dcc509a6f75849b":{"orden":1,"codigo":"002","nombre":"TUBO Y ACCESORIO PVC PRESION"}, "c81e728d9d4c2f636f067f89cc14862c":{"orden":2,"codigo":"003","nombre":"AISLAMIENTO"}, "eccbc87e4b5ce2fe28308fd9f2a7baf3":{"orden":3,"codigo":"004","nombre":"MONTAJE DE AGUA SANITARIA"}, "a87ff679a2f3e71d9181a67b7542122c":{"orden":4,"codigo":"005","nombre":"ABRAZADERAS FONTANERIA"},"e4da3b7fbbce2345d7772b0674a318d5":{"orden":5,"codigo":"006","nombre":"FLEXOS DE ACERO"}, "1679091c5a880faf6fb5e6087eb1b2dc":{"orden":6,"codigo":"007","nombre":"ACCESORIOS DE SANEAMIENTO"}, ... ... ...

This is my code on Android, and I always get null response:

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

                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("Respuesta:"+ response.toString());
                        try {
                            listaCategorias.clear();
                            int array=0;

                            JSONArray categorias = response.getJSONArray("categorias");

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

                                JSONObject objet = categorias.getJSONObject(i);

                                String titulo = objet.getString("nombre");

                                if (isIntegerParseInt(String.valueOf(titulo.charAt(0))))
                                    listaCategorias.add(titulo);

                            }
                            array++;
                            onCallBack.onSuccess(listaCategorias);



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("ErrorRespuesta: "+error.getMessage());

            }
        });
        requestQueue.add(jsonObjectRequest);

Upvotes: 0

Views: 747

Answers (3)

nikhil nangia
nikhil nangia

Reputation: 581

its not an JSON ARRAY Its JSON OBJECT
for your reference I'm posting sample json array

"three_day_forecast": [
    { 
        "conditions": "Partly cloudy",
        "day" : "Monday",
        "temperature": 20 
    },
    { 
        "conditions": "Showers",
        "day" : "Tuesday",
        "temperature": 22 
    },
    { 
        "conditions": "Sunny",
        "day" : "Wednesday",
        "temperature": 28 
    }
]

Data was used by this reference.

Upvotes: 3

Alok Patel
Alok Patel

Reputation: 8022

With reference to the JSON String you've posted categorias is not a JSONArray. it's an object and the JSON string is still invalid.

On the top of the String you have the key as categories, now if you want to use that as an array you must have the value of categories to array which is specified within [ ] in JSON strings.

Another thing is try to avoid random key of the JSON Array if you're using it within loops with indexes. So your JSON String might look like following to support your Android code.

{
"categories": [
     { object 1... },
     { object 2... },
     and so on...
]
}

You can see the type of JSON you will need here:
http://json-parser.com/53b37f69

Upvotes: 0

Ckram
Ckram

Reputation: 586

It's not an Array but a JSONObject you need to modify your JSON to get an array with [] and not {}. Or you get it by getJSONObject.

Upvotes: 0

Related Questions