-1

I have a json returning back to me from server. I am trying to iterate over it but its gving me error on this line

 JSONObject obj = jsonHospitals.getJSONObject(i);

The error underlines in red .getJSONObject(i) and gives me two options 1- change to optJSONObject(...) 2- change type of 'i' to 'String'

What can I do to use JSONObject and iterate over the json. I am new to android so please bear with me fellas! Below is some more of the code

String responseData = new String(charArray);

                    JSONObject jsonHospitals = new JSONObject(responseData);

                    for(int i = 0; i< jsonHospitals.length(); i++){
                        JSONObject obj = jsonHospitals.getJSONObject(i);
                        JSONObject hospital_obj = obj.getJSONObject("Hospital");
                        String hospital_name =  hospital_obj.getString("hospital_name");
                        Log.v(TAG, hospital_name);
                    }

Thanks UPDATE *OK TRIED SOMETHING NEW AND ALL POSTED HERE NOW*

I tried this code

try {

                URL hospitalFeedUrl = new URL("siteresponseaddress");
                HttpURLConnection connection = (HttpURLConnection)hospitalFeedUrl.openConnection();
                connection.connect();
                responseCode = connection.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK){
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);
                    int contentLenght = connection.getContentLength();
                    char[] charArray = new char[contentLenght];
                    reader.read(charArray);
                    String responseData = new String(charArray);

                    JSONArray jsonHospitals = new JSONArray(responseData);

                    for(int i = 0; i< jsonHospitals.length(); i++){
                        JSONObject obj = jsonHospitals.getJSONObject(i);
                        JSONObject hospital_obj = obj.getJSONObject("Hospital");
                        String hospital_name =  hospital_obj.getString("hospital_name");
                        Log.v(TAG, hospital_name);
                    }
                    Log.v(TAG, responseData);
                }
                else {
                    Log.i(TAG, "Unsccessful HHTP Response Code "+ responseCode);
                }
                Log.i(TAG, "Code " + responseCode);
            } 
            catch (MalformedURLException e) {
                Log.e(TAG, "exception caught: ", e);    
            }
            catch (IOException e) {
                Log.e(TAG, "exception IO caught: ", e); 
            }
            catch(Exception e){
                Log.e(TAG, "General exception", e);
            }

NOW thats giving me the following in LOG CAT

02-21 03:13:08.350: D/dalvikvm(838): GC_FOR_ALLOC freed 43K, 5% free 2949K/3072K, paused 153ms, total 154ms
02-21 03:13:08.350: I/dalvikvm-heap(838): Grow heap (frag case) to 3.558MB for 635812-byte allocation
02-21 03:13:08.460: D/dalvikvm(838): GC_FOR_ALLOC freed 2K, 4% free 3567K/3696K, paused 101ms, total 101ms
02-21 03:13:09.310: I/Choreographer(838): Skipped 66 frames!  The application may be doing too much work on its main thread.
02-21 03:13:09.460: D/gralloc_goldfish(838): Emulator without GPU emulation detected.
02-21 03:13:15.110: D/dalvikvm(838): GC_FOR_ALLOC freed 188K, 7% free 3892K/4156K, paused 171ms, total 208ms
02-21 03:13:15.350: D/dalvikvm(838): GC_FOR_ALLOC freed 375K, 13% free 3898K/4464K, paused 60ms, total 79ms
02-21 03:13:15.350: E/MainListActivity(838): General exception
02-21 03:13:15.350: E/MainListActivity(838): org.json.JSONException: Unterminated string at character 21961 of [{"Hospital":{"id":"61790","hospital_name":"Goodland Regional Medical Ctr","hospital_add_1":"220 W 2nd St","hospital_add_2":null,"hospital_city":"Goodland","hospital_state":"KS","hospital_zip":"67735","hospital_phone":"785-890-3625\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61795","hospital_name":"Cheyenne County Hospital","hospital_add_1":"210 W 1st St","hospital_add_2":null,"hospital_city":"Saint Francis","hospital_state":"KS","hospital_zip":"67756","hospital_phone":"785-332-2104\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61801","hospital_name":"Stanton County Hospital","hospital_add_1":"404 N Chestnut St","hospital_add_2":null,"hospital_city":"Johnson","hospital_state":"KS","hospital_zip":"67855","hospital_phone":"620-492-6250\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61802","hospital_name":"Kearny County Hospital","hospital_add_1":"500 E Thorpe St","hospital_add_2":null,"hospital_city":"Lakin","hospital_state":"KS","hospital_zip":"67860","hospital_phone":"620-355-7111\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61803","hospital_name":"Wichita County Health Center","hospital_add_1":"211 E Earl St","hospital_add_2":null,"hospital_city":"Leoti","hospital_state":"KS","hospital_zip":"67861","hospital_phone":"620-375-2233\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61808","hospital_name":"Hamilton County Hospital","hospital_add_1":"700 N Huser St","hospital_add_2":null,"hospital_city":"Syracuse","hospital_state":"KS","hospital_zip":"67878","hospital_phone":"620-384-7461\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61809","hospital_name":"Greeley County Health Services","hospital_add_1":"506 3rd St","hospital_add_2":null,"hospital_city":"Tribune","hospital_state":"KS","hospital_zip":"67879","hospital_phone":"620-376-4221\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61810","hospital_name":"Bob Wilson Memorial Hospital","hospital_add_1":"415 N Main St","hospital_add_2":null,"hospital_city":"Ulysses","hospital_state":"KS","hospital_zip":"67880","hospital_phone":"620-356-1266\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61812","hospital_name":"Morton County Hospital","hospital_add_1":"445 Hilltop St","hospital_add_2":null,"hospital_city":"Elkhart","hospital_state":"KS","hospital_zip":"67950","hospital_phone":"620-697-2141\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61813","hospital_name":"Stevens County Hospital","hospital_add_1":"1006 S Jackson St","hospital_add_2":null,"hospital_city":"Hugoton","hospital_state":"KS","hospital_zip":"67951",

// CATLOG FINISHED

//this is not part of CATLOG JSON

[{"Hospital":{"id":"63083","hospital_name":"Colorado Mental Health Inst","hospital_add_1":"1600 W 24th St","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81003","hospital_phone":"719-546-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"63084","hospital_name":"Parkview Medical Center","hospital_add_1":"400 W 16th St","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81003","hospital_phone":"719-584-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"63085","hospital_name":"St Mary-Corwin Medical Center","hospital_add_1":"1008 Minnequa Ave","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81004","hospital_phone":"719-560-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]}]
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

5 Answers5

0

There is no getJSONObject which takes int as a param.

http://developer.android.com/reference/org/json/JSONObject.html

public JSONObject getJSONObject (String name)

Added in API level 1
Returns the value mapped by name if it exists and is a JSONObject.

Throws
JSONException   if the mapping doesn't exist or is not a JSONObject.

Hence the error. You need to post your json for further help

Your json seems similar to the one in your previous question for which i have already answered @

how to parse this response using JSONObject

Edit:

    try
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     HttpGet request = new HttpGet("your url ");  
     HttpResponse response = httpclient.execute(request);
     HttpEntity resEntity = response.getEntity();
     String _response=EntityUtils.toString(resEntity); 
         JSONArray jr = new JSONArray(_response);
         for(int i=0;i<jr.length();i++)
         {
         JSONObject jb = (JSONObject)jr.getJSONObject(i);
         JSONObject jb1 =(JSONObject) jb.getJSONObject("Hospital");
         String name =  jb1.getString("hospital_name");
         Log.i("name....",name);
         }
    }catch(Exception e)
    {
        e.printStackTrace();
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @BrianRoach maybe. not here http://developer.android.com/reference/org/json/JSONObject.html#getJSONObject(java.lang.String). – Raghunandan Feb 21 '14 at 08:02
  • please see my updat. I have added a detailed information as to what CatLog is saying etc – Asim Zaidi Feb 21 '14 at 08:23
  • @Autolycus can you post the url so that we can see the full json? – Raghunandan Feb 21 '14 at 08:27
  • I will try to copy paste the full json in the question. It is a internal site so the outsiders dont have access to it. Give me 5 minutes – Asim Zaidi Feb 21 '14 at 08:29
  • @Autolycus your json looks exactly like the one in your previos question. So that answer should work for you. you have the error coz you are not reading the full json. – Raghunandan Feb 21 '14 at 08:35
  • no that answer is giving the stack error that I told you already. Please dont refer to that question any more as I am having stack errors that I posted above. thanks – Asim Zaidi Feb 21 '14 at 08:36
  • @Autolycus that is the way to parse the json. its right. the error is not bcoz of the code posted there – Raghunandan Feb 21 '14 at 08:37
0

Change:

JSONObject obj = jsonHospitals.getJSONObject(i);

to:

JSONObject obj = jsonHospitals.getJSONObject(String.valueof(i));

The reason is because the the function getJSONObject expects a String as its Parameter - Link to API Documentation

user1406716
  • 9,565
  • 22
  • 96
  • 151
0

You are trying to iterate a JSONArray Change this line:

JSONObject jsonHospitals = new JSONObject(responseData);

to this

JSONAray jsonHospitals = new JSONAray(responseData);

If it still doesn't work, could you provide your JSON resopnse.

Jackie
  • 307
  • 2
  • 14
0

If your input json is of type JSON-array, you should treat it as such (see doc):

JSONArray array = new JSONArray(responseData);

And then you can iterate over it, using:

for(int i = 0; i< array.length(); i++) {
  array.getJSONObject(i);
}
sebdehne
  • 332
  • 3
  • 7
-1
 Hi Autolycus,
             I think you have to parse  responseData as JSONArray like below

            **JSONArray jsonHospitals = new JSONArray(responseData);**

          for(int i = 0; i< jsonHospitals.length(); i++){
                    JSONObject obj = jsonHospitals.getJSONObject(i);
                    JSONObject hospital_obj = obj.getJSONObject("Hospital");
                    String hospital_name =  hospital_obj.getString("hospital_name");
                    Log.v(TAG, hospital_name);
                }

       Hope it will work for you :)
Deo
  • 82
  • 5