MoonLight
MoonLight

Reputation: 557

Iterate A JSON Object containing list/array in android Studio

I have a JSON object that looks like this (will be returned from flask API to android Studio),

[
  {
    "Drug_Name": "Lepirudin"
  }, 
  {
    "Drug_Name": "Cetuximab"
  }, 
  {
    "Drug_Name": "Geptanolimab"
  }, 
  {
    "Drug_Name": "Narsoplimab"
  }, 
  {
    "Drug_Name": "Ieramilimab"
  }, 
  {
    "Drug_Name": "Vibostolimab"
  }, 
  {
    "Drug_Name": "Volagidemab"
  }, 
  {
    "Drug_Name": "Quavonlimab"
  }, 
  {
    "Drug_Name": "AK119"
  }, 
  {
    "Drug_Name": "Allocetra"
  }
]

This is how am getting this data in Flask app

            myCursor=myConnection.cursor()
            #query
            SQLcommand="SELECT Drug_Name FROM Drug_Description"
            
            #executing above query
            myCursor.execute(SQLcommand)  
                 
            
            row_headers=[x[0] for x in myCursor.description] #this will extract row headers
            rv = myCursor.fetchall()
            json_data=[]
            for result in rv:
                json_data.append(dict(zip(row_headers,result)))
            return jsonify (json_data)

This is how I am getting a json object in android Studio

String url = "https://********.herokuapp.com/";                
  RequestQueue queue = Volley.newRequestQueue(getContext());                 
  JsonObjectRequest jsonObjectRequest = new JsonObjectRequest            
  (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                
                                                @Override
                                                // When It works
                                                public void onResponse(JSONObject response) {
                                                    try {
    //get the json object and iterate it here
                                                }, new Response.ErrorListener() {
                                                @Override
                                                // If it doesnt work what to do (Errors)
                                                public void onErrorResponse(VolleyError error) {
                                                Toast.makeText(getContext(),"Try 
                                                Again",Toast.LENGTH_LONG).show();
        }
                                    });
                            queue.add(jsonObjectRequest);

How can I iterate over these values and save each drug name in a string array in android studio (java).

Upvotes: 0

Views: 199

Answers (1)

Abhishek Dubey
Abhishek Dubey

Reputation: 945

    private void getdata(){   
    String url = "https://********.herokuapp.com/";                
    RequestQueue queue = Volley.newRequestQueue(getContext());   
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {    
        @Override    
        public void onResponse(JSONArray itemArray) {  
         List<String> allDrugs = new ArrayList<String>();
         for (int i=0; i<itemArray.length(); i++) {
         JSONObject data = itemArray.getJSONObject(i);
         String name = data.getString("Drug_Name");
         allDrugs.add(name);
        }
        }    
    }, new Response.ErrorListener() {    
        @Override    
        public void onErrorResponse(VolleyError error) {    
            Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();    
        }    
    });  
    requestQueue.add(jsonArrayRequest);  
} 

Slight modifications might be required but more or less you can use this function.

Upvotes: 1

Related Questions