我有一个json从服务器返回给我。我试图迭代它,但它在这行上给我带来了错误
JSONObject obj = jsonHospitals.getJSONObject(i);
错误以红色.getJSONObject(i)下划线并给出了两个选项 1-更改为optJSONObject(...) 2-将'i'的类型改为'String'
如何使用JSONObject并迭代json。我是android的新手,所以请耐心跟我说说! 下面是一些代码
String responseData = new String(charArray);
JSONObject jsonHospitals = new JSONObject(responseData);
for(int i = 0; i< jsonHospitals.length(); i++){
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject hospital_obj = obj.getJSONObject("Hospital");
String hospital_name = hospital_obj.getString("hospital_name");
Log.v(TAG, hospital_name);
}
由于 的更新 * 好的尝试新的,现在全部张贴在这里 *
我试过这段代码
try {
URL hospitalFeedUrl = new URL("siteresponseaddress");
HttpURLConnection connection = (HttpURLConnection)hospitalFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLenght = connection.getContentLength();
char[] charArray = new char[contentLenght];
reader.read(charArray);
String responseData = new String(charArray);
JSONArray jsonHospitals = new JSONArray(responseData);
for(int i = 0; i< jsonHospitals.length(); i++){
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject hospital_obj = obj.getJSONObject("Hospital");
String hospital_name = hospital_obj.getString("hospital_name");
Log.v(TAG, hospital_name);
}
Log.v(TAG, responseData);
}
else {
Log.i(TAG, "Unsccessful HHTP Response Code "+ responseCode);
}
Log.i(TAG, "Code " + responseCode);
}
catch (MalformedURLException e) {
Log.e(TAG, "exception caught: ", e);
}
catch (IOException e) {
Log.e(TAG, "exception IO caught: ", e);
}
catch(Exception e){
Log.e(TAG, "General exception", e);
}
现在在LOG CAT中给我以下内容
02-21 03:13:08.350: D/dalvikvm(838): GC_FOR_ALLOC freed 43K, 5% free 2949K/3072K, paused 153ms, total 154ms
02-21 03:13:08.350: I/dalvikvm-heap(838): Grow heap (frag case) to 3.558MB for 635812-byte allocation
02-21 03:13:08.460: D/dalvikvm(838): GC_FOR_ALLOC freed 2K, 4% free 3567K/3696K, paused 101ms, total 101ms
02-21 03:13:09.310: I/Choreographer(838): Skipped 66 frames! The application may be doing too much work on its main thread.
02-21 03:13:09.460: D/gralloc_goldfish(838): Emulator without GPU emulation detected.
02-21 03:13:15.110: D/dalvikvm(838): GC_FOR_ALLOC freed 188K, 7% free 3892K/4156K, paused 171ms, total 208ms
02-21 03:13:15.350: D/dalvikvm(838): GC_FOR_ALLOC freed 375K, 13% free 3898K/4464K, paused 60ms, total 79ms
02-21 03:13:15.350: E/MainListActivity(838): General exception
02-21 03:13:15.350: E/MainListActivity(838): org.json.JSONException: Unterminated string at character 21961 of [{"Hospital":{"id":"61790","hospital_name":"Goodland Regional Medical Ctr","hospital_add_1":"220 W 2nd St","hospital_add_2":null,"hospital_city":"Goodland","hospital_state":"KS","hospital_zip":"67735","hospital_phone":"785-890-3625\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61795","hospital_name":"Cheyenne County Hospital","hospital_add_1":"210 W 1st St","hospital_add_2":null,"hospital_city":"Saint Francis","hospital_state":"KS","hospital_zip":"67756","hospital_phone":"785-332-2104\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61801","hospital_name":"Stanton County Hospital","hospital_add_1":"404 N Chestnut St","hospital_add_2":null,"hospital_city":"Johnson","hospital_state":"KS","hospital_zip":"67855","hospital_phone":"620-492-6250\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61802","hospital_name":"Kearny County Hospital","hospital_add_1":"500 E Thorpe St","hospital_add_2":null,"hospital_city":"Lakin","hospital_state":"KS","hospital_zip":"67860","hospital_phone":"620-355-7111\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61803","hospital_name":"Wichita County Health Center","hospital_add_1":"211 E Earl St","hospital_add_2":null,"hospital_city":"Leoti","hospital_state":"KS","hospital_zip":"67861","hospital_phone":"620-375-2233\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61808","hospital_name":"Hamilton County Hospital","hospital_add_1":"700 N Huser St","hospital_add_2":null,"hospital_city":"Syracuse","hospital_state":"KS","hospital_zip":"67878","hospital_phone":"620-384-7461\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61809","hospital_name":"Greeley County Health Services","hospital_add_1":"506 3rd St","hospital_add_2":null,"hospital_city":"Tribune","hospital_state":"KS","hospital_zip":"67879","hospital_phone":"620-376-4221\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61810","hospital_name":"Bob Wilson Memorial Hospital","hospital_add_1":"415 N Main St","hospital_add_2":null,"hospital_city":"Ulysses","hospital_state":"KS","hospital_zip":"67880","hospital_phone":"620-356-1266\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61812","hospital_name":"Morton County Hospital","hospital_add_1":"445 Hilltop St","hospital_add_2":null,"hospital_city":"Elkhart","hospital_state":"KS","hospital_zip":"67950","hospital_phone":"620-697-2141\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61813","hospital_name":"Stevens County Hospital","hospital_add_1":"1006 S Jackson St","hospital_add_2":null,"hospital_city":"Hugoton","hospital_state":"KS","hospital_zip":"67951",
// CATLOG FINISHED
//这不是CATLOG的一部分 的 JSON
[{"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":[]}]
答案 0 :(得分:0)
没有getJSONObject
将int作为参数。
http://developer.android.com/reference/org/json/JSONObject.html
public JSONObject getJSONObject (String name)
Added in API level 1
Returns the value mapped by name if it exists and is a JSONObject.
Throws
JSONException if the mapping doesn't exist or is not a JSONObject.
因此错误。您需要发布您的json以获得进一步的帮助
你的json看起来和你上一个问题的json相似,我已经回答了@
how to parse this response using JSONObject
编辑:
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("your url ");
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity);
JSONArray jr = new JSONArray(_response);
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);
}
}catch(Exception e)
{
e.printStackTrace();
答案 1 :(得分:0)
变化:
JSONObject obj = jsonHospitals.getJSONObject(i);
为:
JSONObject obj = jsonHospitals.getJSONObject(String.valueof(i));
原因是因为函数getJSONObject需要一个String作为其参数 - Link to API Documentation
答案 2 :(得分:0)
您正在尝试迭代JSONArray 改变这一行:
JSONObject jsonHospitals = new JSONObject(responseData);
到这个
JSONAray jsonHospitals = new JSONAray(responseData);
如果它仍然不起作用,你能否提供你的JSON resopnse。
答案 3 :(得分:0)
如果你的输入json是JSON数组类型,你应该这样对待它(见doc):
JSONArray array = new JSONArray(responseData);
然后你可以使用:
迭代它for(int i = 0; i< array.length(); i++) {
array.getJSONObject(i);
}
答案 4 :(得分:-1)
Hi Autolycus,
I think you have to parse responseData as JSONArray like below
**JSONArray jsonHospitals = new JSONArray(responseData);**
for(int i = 0; i< jsonHospitals.length(); i++){
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject hospital_obj = obj.getJSONObject("Hospital");
String hospital_name = hospital_obj.getString("hospital_name");
Log.v(TAG, hospital_name);
}
Hope it will work for you :)