[英]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": "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 * 编辑JSON * 更新JSON *
[ // 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
You won't- its invalid JSON. 您不会-无效的JSON。 You're missing most of your "" around field names.
您缺少字段名称中的大部分“”。
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");
}
我建议您使用fastjson( https://github.com/alibaba/fastjson )。
Checkout this cool libray for parsing JSOn in Android its called GSON https://code.google.com/p/google-gson/ . 签出这个很棒的库,用于在Android中解析JSOn,它名为GSON https://code.google.com/p/google-gson/ 。 Via this parsing this very simple.
通过这个解析,这非常简单。
Your string is not a valid JSON object. 您的字符串不是有效的JSON对象。 Check out jsonlint before trying to parse a string as JSON.
在尝试将字符串解析为JSON之前,请先检查jsonlint 。 After which, you can read about parsing JSON in Android .
之后,您可以阅读有关在Android中解析JSON的信息 。 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. 使用内置的
org.json
足够容易,但是如果您使用那里的许多Java库之一进一步简化它,那么应该会容易得多。 You can look into Jackson or google-gson , two of the most capable utilities for your purpose. 您可以研究Jackson或google-gson ,这两种功能最强大的实用程序可满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.