1

I am trying to parse my json with:

for(int i = 0; i < json.getJSONArray("JSON").length(); i++) {
                String taste = json.getJSONArray("JSON").getJSONObject(i).getString("taste");
                String rate = json.getJSONArray("JSON").getJSONObject(i).getString("rate");
                int foo = Integer.parseInt(rate);
                count = count + foo;


                //create object
                BeerTastes tempTaste = new BeerTastes(taste, rate);

                //add to arraylist
                tasteList.add(tempTaste);

                Log.d("taste",tempTaste.taste);
                Log.d("number",tempTaste.percent);
            }

But my logs at the end are not outputting anything at all so I assume I am not parsing my json correctly. The json I am looking at is:

[{"taste":"Bitter","rate":"13"},{"taste":"Malty","rate":"3"},{"taste":"Smooth","rate":"3"},{"taste":"Dry","rate":"1"}]

I think I may be wrong with:

json.getJSONArray("JSON") 

because my array doesnt have a name but I it has to take a string...

Mike
  • 6,751
  • 23
  • 75
  • 132
  • That _is_ a JSONArray. – SLaks Jun 27 '13 at 00:45
  • I know, thats why I use getJSONArray() not sure what your trying to point out. Maybe try and be a little more specific. – Mike Jun 27 '13 at 00:47
  • This is a [duplicate of this question](http://stackoverflow.com/questions/10164741/get-jsonarray-without-array-name). As @SLaks hints at, delete every `.getJSONArray("JSON")` and it should work. – Ken Y-N Jun 27 '13 at 00:54

1 Answers1

3

I find no 'JSON' in your JSON String.So i think you should write like this:

JSONArray jsonArray = new JSONArray(json);

then:

for(int i = 0; i < jsonArray.length(); i++) {
            String taste = jsonArray.getJSONObject(i).getString("taste");
            String rate = jsonArray.getJSONObject(i).getString("rate");
            int foo = Integer.parseInt(rate);
            count = count + foo;


            //create object
            BeerTastes tempTaste = new BeerTastes(taste, rate);

            //add to arraylist
            tasteList.add(tempTaste);

            Log.d("taste",tempTaste.taste);
            Log.d("number",tempTaste.percent);
        }
Jave
  • 46
  • 3