0

I have one url, I want to take these data and show it into list view using JSON, but when I run it, there's something error, that said :

JSONArray cannot be converted to JSONObject

This is my code:

        JSONArray jsonArray = new JSONArray ();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray innerJsonArray = jsonArray.getJSONArray(i);
            JSONObject c = innerJsonArray.getJSONObject(0);

            HashMap<String, String> map = new HashMap<String, String>();
            // Storing each json item in variable
            map.put("atasan", c.get("atasan").toString());
            map.put("kode_agen", c.get("kode_agen").toString());
            map.put("jenis", c.get("jenis").toString());
            map.put("no_aaji", c.get("no_aaji").toString());
            map.put("nama_agen", c.get("nama_agen").toString());

            AgenList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

ListAdapter adapter = new SimpleAdapter(
            MainActivity.this, AgenList, R.layout.list_item,
            new String[] {ATASAN, KODE_AGEN, JENIS, NO_AAJI, NAMA_AGEN}, new int [] {R.id.atasan, R.id.kode_agen, R.id.jenis, R.id.no_aaji, R.id.nama_agen});

setListAdapter(adapter);

I don't know where's my fault, I hope somebody can help me to solve this problem ?

this is the example data that i want to take : {"atasan":"THOMAS SUNARDI ", "kode_agen":"024932", "jenis":"Regional","no_aaji":"11529943* " ,"nama_agen":"YONATHAN ADRIYANTO WIDJAJA"}

Aoyama Nanami
  • 2,532
  • 9
  • 32
  • 50

2 Answers2

2

so do this..

JSONObject jsonObj= new JSONObject(new String(buffer));

JSONArray data = jsonObj.getJSONArray("data");

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

}
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
0

I have not seen the JSON yet but I presume that the error must be on this line

JSONObject c = innerJsonArray.getJSONObject(0);

try this:

JSONArray c = innerJsonArray.getJSONArray(0);
Mukund Samant
  • 1,079
  • 7
  • 12
  • JSON from web service as like this : [{"atasan":"THOMAS SUNARDI ", "kode_agen":"024932", "jenis":"Regional", "no_aaji":"11529943* ", "nama_agen":"YONATHAN ADRIYANTO WIDJAJA"}, {.... ...... ...... ......}] – Aoyama Nanami Apr 02 '13 at 04:11