-7

I'm new to Android and I have tried so many options to access the JSONObject which returns from an API call but I couldn't succeed as any of the solutions i looked for didn't work for me.

What i want is to access the JSONObject and keep the Id & Name in a Array. And then populate the Names in a AutoCompleteTextView. How do i properly access the JSONObject. Please help me with this. I'm stuck on this for more than a day.

Following is my Code handling the JSONObject.

@Override
public void processFinish(JSONObject output) {
    Toast.makeText(MainActivity.this,"ProcessFinish",Toast.LENGTH_SHORT).show();
    allStations = output;

    if(output != null){
        Toast.makeText(MainActivity.this,output.toString(),Toast.LENGTH_SHORT).show();



    }else{
        Toast.makeText(MainActivity.this," Connection Failed!",Toast.LENGTH_SHORT).show();
    }
}

Following is a sample output of my JSONObject

{
    "SUCCESS": true,
    "MESSAGE": "Found 398 Results!",
    "NOFRESULTS": 3,
    "RESULTS": {
        "stationList": [
            {
                "stationCode": "ABN",
                "stationID": 3,
                "stationName": "ABLA"
            },
            {
                "stationCode": "ADLA",
                "stationID": 410,
                "stationName": "ADLA"
            },
            {
                "stationCode": "ANM",
                "stationID": 11,
                "stationName": "AHAMA"
            }]
    },
    "STATUSCODE": "2000"
}

3 Answers3

1

try this

try {
JSONObject obj= output.getJSONObject("RESULTS");

JSONArray dataArray= obj.getJSONArray(“stationList“);

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

    JSONObject object1=dataArray.getJSONObject(i);
    Strind id = object1.getString("stationID");
}
} catch (JSONException e) {

    e.printStackTrace();

}

In This code output is your JSONObject result

Sagar Vasoya
  • 158
  • 1
  • 12
0

try this

try {
       JSONObject jsonObject = new JSONObject("response");
       boolean status= jsonObject.getBoolean("SUCCESS");
       String MESSAGE= jsonObject.getString("MESSAGE");
       String NOFRESULTS= jsonObject.getString("NOFRESULTS");
       String STATUSCODE= jsonObject.getString("STATUSCODE");


       JSONObject obj=jsonObject.getJSONObject("RESULTS");
       JSONArray jsonarray = obj.optJSONArray("stationList");
       for (int i = 0; i < jsonarray.length(); i++){
            JSONObject json_data = jsonarray.getJSONObject(i);
            Log.e("stationCode",json_data.getString("stationCode"));
            Log.e("stationID",json_data.getString("stationID"));
            Log.e("stationName",json_data.getString("stationName"));
       }
     } catch (JSONException e) {
            e.printStackTrace();
   }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Have you tried using a JSON Array? For example you could use this method for storage:

JSONObject wgroup = new JSONObject();   //FINAL json object 

try { //put initial data
    wgroup.put("id", "2");
    wgroup.put("user", "someone");
    wgroup.put("stime", "2017-02-06 16:30:13");
    wgroup.put("etime", "2017-02-06 19:30:13");
    wgroup.put("real_dur", 3600); 

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

JSONArray jsonArray1 = new JSONArray(); //Create an array to store ALL Variables

for (int y=0; y< your_array.length ; y++ ){ //loop through your information array 
JSONObject output = new JSONObject();  //CREATE a json object to put 1 workout
try {
    wgroup.put("id", "2");
    wgroup.put("name", "sam");
    wgroup.put("age", "3");
    wgroup.put("gender", "male");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
JSONArray jsonArray2 = new JSONArray();  //CREATE a json array to put 1 array

jsonArray1.put(output);  //insert this OBJECT into the ARRAY
}
wgroup.put(jsonArray1);//insert the workouts ARRAY into the original object