Skip to content
Advertisement

in android jsonarray cannot be converted in jsonobject

The script fetches data from phpmyadmin but android studio cause this error

of type org.json.Jsonarray cannot be converted to jsonobject

enter image description here

my code android

code

Advertisement

Answer

Actually you are getting JSONArray instead of JSONObject from your server. So, You have to parse the array first then you can get object from it.

JSONArray jsonArray = new JSONArray(response);

for(int i = 0; i < jsonArray.length(); i++) {
     JSONObject jsonObject = jsonArray.getJSONObject(i);

     String id = jsonObject.getString("id"); 
     String m_e = jsonObject.getString("M_E"); 
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement