Skip to content Skip to sidebar Skip to footer

How To Parse This Response Using JSONObject

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': '

Solution 1:

[ // 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

Solution 2:

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


Solution 3:

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");
}

Solution 4:

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


Solution 5:

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.


Post a Comment for "How To Parse This Response Using JSONObject"