0

I have this response that I get back form server. I want to parse it and get the hospital_name out of it. How would I go about it?

[
    {
        "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": []
    }
]

EDITED THE JSON *UPDATED JSON *

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

6 Answers6

2
[ // json array node 
{  // json object node 
"Hospital": { // json object Hospital 

To parse

JSONArray jr = new JSONArray("jsonstring");
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);
}

Log

02-18 03:09:43.950: I/name....(951): Colorado Mental Health Inst
02-18 03:09:43.950: I/name....(951): Parkview Medical Center
02-18 03:09:43.950: I/name....(951): St Mary-Corwin Medical Center
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

You won't- its invalid JSON. You're missing most of your "" around field names.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

Try this..

JSONArray tot_array = new JSONArray(response);

for(int i = 0; i< tot_array.length(); i++){
    JSONObject obj = tot_array.getJSONObject(i);
    JSONObject hospital_obj = obj.getJSONObject("Hospital");
    String hospital_name =  hospital_obj.getString("hospital_name");
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

I recommend you use the fastjson (https://github.com/alibaba/fastjson).

WBL
  • 31
  • 3
0

Checkout this cool libray for parsing JSOn in Android its called GSON https://code.google.com/p/google-gson/ . Via this parsing this very simple.

Code_Life
  • 5,742
  • 4
  • 29
  • 49
  • I know its not the direct answer , but just checkout this lib u will get directly get list of Hospitals as output. – Code_Life Feb 18 '14 at 07:27
  • I am following a tutorial so I want to stay on the track and use what they are using. thanks – Asim Zaidi Feb 18 '14 at 07:27
  • okies .... everytime looping ur json is painful and is more prone to error .. just check this out .. https://sites.google.com/site/gson/gson-user-guide .. :) – Code_Life Feb 18 '14 at 07:31
0

Your string is not a valid JSON object. Check out jsonlint before trying to parse a string as JSON. After which, you can read about parsing JSON in Android. It's easy enough with the built-in org.json, but should be much easier if you use one of the many Java libraries out there that simplifies it further. You can look into Jackson or google-gson, two of the most capable utilities for your purpose.

josephus
  • 8,284
  • 1
  • 37
  • 57