how to parse this response using JSONObject

孤街醉人 提交于 2019-12-25 03:15:42

问题


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 *


回答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



回答2:


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




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



回答4:


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




回答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.




回答6:


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.



来源:https://stackoverflow.com/questions/21846806/how-to-parse-this-response-using-jsonobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!