Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSONObject给我错误_Java_Android_Android Layout_Android Intent - Fatal编程技术网

Java JSONObject给我错误

Java JSONObject给我错误,java,android,android-layout,android-intent,Java,Android,Android Layout,Android Intent,我有一个json从服务器返回给我。我试图迭代它,但这一行的gving me错误 JSONObject obj = jsonHospitals.getJSONObject(i); 错误以红色下划线显示。getJSONObject(i)并提供了两个选项 1-更改为optJSONObject(…) 2-将“i”的类型更改为“String” 如何使用JSONObject并迭代json。我是android新手,所以请大家耐心点! 下面是更多的代码 String responseData = new S

我有一个json从服务器返回给我。我试图迭代它,但这一行的gving me错误

 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);
                    }
//CATLOG已完成

//这不是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":[]}]

没有将int作为参数的
getJSONObject

因此出现了错误。您需要发布json以获得进一步帮助

您的json似乎与前面问题中的json相似,我已经回答了这个问题@

编辑:

试试看
{
HttpClient HttpClient=新的DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpGet请求=新建HttpGet(“您的url”);
HttpResponse response=httpclient.execute(请求);
HttpEntity当前性=response.getEntity();
字符串_response=EntityUtils.toString(最近性);
JSONArray jr=新JSONArray(_响应);
对于(int i=0;i变化:

JSONObject obj = jsonHospitals.getJSONObject(i);
致:


原因是函数getJSONObject需要一个字符串作为其参数-

您正在尝试迭代JSONArray 更改此行:

JSONObject jsonHospitals=新JSONObject(responseData)

对此

JSONAray jsonHospitals=新的JSONAray(responseData)


如果仍然不起作用,您能否提供您的JSON解决方案。

如果您的输入JSON类型为JSON array,则应将其视为JSON array(请参阅):

然后您可以使用以下命令对其进行迭代:

for(int i = 0; i< array.length(); i++) {
  array.getJSONObject(i);
}
for(int i=0;i
你好,Autolycus,
我认为您必须将responseData解析为如下所示的JSONArray
**JSONArray jsonHospitals=新的JSONArray(responseData)**
for(int i=0;i
@BrianRoach可能吧。不在这里。请查看我的更新。我已经添加了关于CatLog所说内容的详细信息etc@Autolycus你能发布url以便我们能看到完整的json吗?我会尝试复制粘贴问题中的完整json。这是一个内部网站,因此外人无法访问它。给我5minutes@Autolycus你的厕所ks与您的previos问题中的一个完全相同。因此,该答案应该适用于您。您有错误,因为您没有阅读完整的json。请发布一个您试图解析的json示例。不清楚您是否真的有json数组或对象。@BrianRoach我猜它与他之前的问题相关@,答案告诉请看更新!谢谢!我看到catlog在抱怨解析
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.
    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();
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject obj = jsonHospitals.getJSONObject(String.valueof(i));
JSONArray array = new JSONArray(responseData);
for(int i = 0; i< array.length(); i++) {
  array.getJSONObject(i);
}
 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 :)