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

7
  • Is it possible to paste your JSON? Commented Apr 1, 2013 at 8:19
  • you have to get the data using array not object .. Commented Apr 1, 2013 at 8:29
  • Aoyama Nanami Is it working for you!!! Commented Apr 1, 2013 at 9:21
  • @Aoyama Nanami if my answer help you then accept the answer!! Commented Apr 2, 2013 at 7:11
  • @janmejoy your answer can't help me, it doesn't work Commented Apr 2, 2013 at 9:17

2 Answers 2

2

so do this..

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

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

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

}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

JSON from web service as like this : [{"atasan":"THOMAS SUNARDI ", "kode_agen":"024932", "jenis":"Regional", "no_aaji":"11529943* ", "nama_agen":"YONATHAN ADRIYANTO WIDJAJA"}, {.... ...... ...... ......}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.