简体   繁体   中英

Error parsing data org.json.JSONException in android

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"}

so do this..

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

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

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

}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM