Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get JSON array in Child

I'm beginner in Android Studio, and I am a bit difficult to parse json data in Android, so I want to ask question about get or parsing JSON Child Array.

This is my Code :

public void resSuccess(String requestType, JSONObject response) 
                       { progressBar.setVisibility(View.GONE);
try {
token = response.getString("token");
JSONArray array = response.getJSONArray("all_airport");
for (int i=0; i<array.length(); i++){
    JSONObject jsonObject = array.getJSONObject(i);
    JSONArray jsonArray = jsonObject.getJSONArray("airport");
    for (int j=0; j<jsonArray.length(); j++) {
        JSONObject object = jsonArray.getJSONObject(j);
        BandaraDataSet bds = new BandaraDataSet();
        idDep = object.getString("country_id");
        bds.setId(object.getString("id"));
        bds.setAirport_name(object.getString("airport_name"));
        bds.setAirport_code(object.getString("airport_code"));
        bds.setCountry_name(object.getString("country_name"));
        bds.setCountry_id(object.getString("country_id"));
        bds.setLocation_name(object.getString("location_name"));
        list.add(bds);
    }
}
bandaraAdapter = new BandaraAdapter(ActivityPesawat.this, list);
bandaraAdapter.notifyDataSetChanged();
listBandara.setAdapter(bandaraAdapter);
} catch (Exception e){
      e.printStackTrace();
}
}

And This is my Json

{
    "all_airport":{
        "airport":[
                {
                "airport_name":"Mali",
                "airport_code":"ARD",
                "location_name":"Alor Island",
                "country_id":"id",
                "country_name":"Indonesia"
                },
                {
                "airport_name":"Pattimura",
                "airport_code":"AMQ",
                "location_name":"Ambon",
                "country_id":"id",
                "country_name":"Indonesia"
                },
                {
                "airport_name":"Tanjung Api",
                "airport_code":"VPM",
                "location_name":"Ampana",
                "country_id":"id",
                "country_name":"Indonesia"
                }
            ]
        },
    "token":"ab4f5e12e794ab09d49526bc75cf0a0139d9d849",
    "login":"false"
}

so my problem when Parse Json is null in Android, please help anyone..

like image 742
Dayat San Avatar asked Dec 05 '25 12:12

Dayat San


1 Answers

You are handling the JSONObject as if it were a JSONArray. Try this code:

public void resSuccess(String requestType, JSONObject response) { 
    progressBar.setVisibility(View.GONE);
    try {
        token = response.getString("token");
        JSONObject airports = response.getJSONObject("all_airport");

        JSONArray airportArray = airports.getJSONArray("airport");
        for (int j = 0; j < airportArray.length(); j++) {
            BandaraDataSet bds = new BandaraDataSet();
            JSONObject object = airportArray.getJSONObject(j);
            idDep = object.getString("country_id");
            bds.setId(object.getString("id"));
            bds.setAirport_name(object.getString("airport_name"));
            bds.setAirport_code(object.getString("airport_code"));
            bds.setCountry_name(object.getString("country_name"));
            bds.setCountry_id(object.getString("country_id"));
            bds.setLocation_name(object.getString("location_name"));
            list.add(bds);
        }

        bandaraAdapter = new BandaraAdapter(ActivityPesawat.this, list);
        bandaraAdapter.notifyDataSetChanged();
        listBandara.setAdapter(bandaraAdapter);
    } catch (Exception e){
        e.printStackTrace();
    }
}
like image 145
Barns Avatar answered Dec 07 '25 16:12

Barns