如何使用JSONObject解析此响应

时间:2014-02-18 07:22:01

标签: java android android-layout android-listview

我有这个回复,我回到表单服务器。我想解析它并从中获取hospital_name。我该怎么做呢?

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

编辑JSON * 更新的JSON *

6 个答案:

答案 0 :(得分:2)

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

解析

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

日志

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

答案 1 :(得分:2)

你不会 - 它的无效JSON。你遗漏了大部分字段名称。

答案 2 :(得分:1)

试试这个..

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

答案 3 :(得分:1)

我建议你使用fastjson(https://github.com/alibaba/fastjson)。

答案 4 :(得分:0)

检查这个很酷的库,在Android中解析JSOn,称为GSON https://code.google.com/p/google-gson/。通过这个解析这很简单。

答案 5 :(得分:0)

您的字符串不是有效的JSON对象。在尝试将字符串解析为JSON之前,请查看jsonlint。之后,您可以阅读parsing JSON in Android。使用内置的org.json很容易,但是如果你使用其中一个Java库来进一步简化它,应该会容易得多。您可以查看Jacksongoogle-gson这两个功能最强大的实用程序。