2015-09-07 83 views
-3

我得到通过凌空一个JSON响应,响应如下:JSON解析的Android JSON数组和JSON对象

{ 
    "status":"success", 
    "message":"Request Successfull", 
    "messagetitle":"Success", 
    "show":"false", 
    "goback":"false", 
    "companies":[ 
     { 
     "id":"14", 
     "category":{ 
      "id":"1", 
      "name":"test" 
     }, 
     "subcategory":{ 
      "id":"1", 
      "name":"test", 
      "image":"https:\/\/page.com\/page\/uploads\/pagepics\/test\/testpics\/test.jpg" 
     }, 
     "name2":"Company", 
     "location":null, 
     "logo":"https:\/\/page.com\/test\/testp\/testpics\/logo.png", 
     "picture":"https:\/\/page.com\/test\/", 
     "facebook":"https:\/\/www.facebook.com\/test", 
     "instagram":"", 
     "twitter":"", 
     "telephone1":"+9611990454", 
     "telephone2":"+961000000", 
     "address":"Lebanon", 
     "longitude":"0", 
     "latitude":"0", 
     "website":"www.website.com", 
     "email":"", 
     "desc":"asdasdas das das das dasda das das das dsadasdsadasdsad asd asd asd asd sad sa as das dsa asd das a a", 
     "user":{ 
      "id":"21", 
      "name":"X Y" 
     }, 
     "status":"1" 
     }, 
     { 
     "id":"4", 
     "category":{ 
      "id":"1", 
      "name":"test" 
     }, 
     "subcategory":{ 
      "id":"1", 
      "name":"test", 
      "image":"https:\/\/page.com\/test\/testp\/testpics\/test\/testphoto\/test.jpg" 
     }, 
     "name2":"Your Company", 
     "location":null, 
     "logo":"", 
     "picture":"https:\/\/page.com\/test\/", 
     "facebook":"", 
     "instagram":"", 
     "twitter":"", 
     "telephone1":"", 
     "telephone2":"", 
     "address":"", 
     "longitude":"0", 
     "latitude":"0", 
     "website":"", 
     "email":"", 
     "desc":"", 
     "user":{ 
      "id":"1", 
      "name":"X Y" 
     }, 
     "status":"1" 
     } 
    ] 
} 

我试图让所有的“形象”和“NAME2”对象响应。这是我的java代码:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, 
       link, null, 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
      //   Log.d("Request", response.toString()); 
         try { 

           jarray1 = response.getJSONArray("subcategory"); 

for(int i=0;i<jarray1.length();i++) 
{ 
    JSONObject object = (JSONObject) jarray1.get(i); 



    String url = object.getString("image"); 
    String name= object.getString("name2"); 
    Toast.makeText(getBaseContext(),url+"\n"+name,Toast.LENGTH_LONG).show(); 
    } 



         } catch (JSONException e) { 
          e.printStackTrace(); 

         } 

        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(jsonObjReq); 
    } 

那么这是给我错误的信息,请任何帮助吗?

+0

请让我知道你得到的错误,因为Facebook的属性是错误的我的意思是你需要添加“在Facebook之前 –

+0

当我运行它在for循环,它给了我相同的信息重复,它没有进入对象和提取字符串。任何帮助? –

+1

为了上帝的爱和圣洁,请格式化代码 –

回答

0
JSONArray jAry=response.getJSONArray("companies"); 
for(int k=0;k<jAry.size();k++) 
{ 
    String url = jAry.getJSONObject(k).getString("image"); 
    String name= jAry.getJSONObject(k).getString("name2"); 
} 

你必须像上面的代码一样使用json的输出。我给它例如

+2

* sry如果有任何错字*为什么不在发布前检查你的错误答案? –

+0

@Tim现在我编辑了我的帖子,我的意思是我的英文错字 –

+0

@ 2Dee我不能明确地告诉你 –

1

你不能直接访问内部对象JSON,根据您的JSON格式,你必须先得到公司

JSONArray companiesArray = response.getJSONArray("companies"); 

的JSON数组,那么得到的数组中的元素

for(int i=0;i<companiesArray .length();i++){ 
    JSONObject object = (JSONObject) companiesArray.get(i); 
    // Now get subcategory information as JSONObject 
    JSONObject subcategoryObject =object .getJSONObject("subcategory"); 
    // Now you can get image and name from subcategoryObject 
    String url = subcategoryObject.getString("image"); 
    String name= subcategoryObject.getString("name2"); 
} 
+0

它没有工作,它给了我第一个节点很多次 –

+0

@MonzerYaghi第一个节点?? –

0
public void SendPost() { 

    HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost("http://your_php_url"); 

     try { 

      httppost.setEntity(new UrlEncodedFormEntity(postdata)); // Here postdata is BasicNameValuePair array with data to send 
      HttpResponse httpResponse = httpclient.execute(httppost); 
      String result = EntityUtils.toString(httpResponse.getEntity()); 

      GetResponce(result); 

     } catch (UnsupportedEncodingException ex) { 

     } catch (IOException ex) { 

     } catch (JSONException ex) { 

     } 

} 

public void GetResponce(String responce) throws JSONException { 

    JSONObject myObject = new JSONObject(responce); 

    JSONArray companies = myObject.getJSONArray("companies"); 

    String[] urls = new String[companies.length()]; 
    String[] names = new String[companies.length()]; //Here i'm initializing the arrays 

    for (int i = 0; i < companies.length(); i++) { 

     JSONObject object = companies.getJSONObject(i); 

     JSONObject secondObject = object.getJSONObject("subcategory"); 

     urls[i] = secondObject.getString("image"); 

     names[i] = object.getString("name2"); 

    } 

} 
+0

它没有工作,这是多次给我的第一个节点 –

+0

对不起,我会再试一次,当我完成时更新我的​​答案:) –

+0

你确定你发送你的数据rigth,因为它适用于我。如果您在循环内部使用String url和String名称进行操作,它将只显示最后一条记录,因为它会在每个循环中覆盖这些变量。你可以通过在数组中排序来防止这种情况 - 例如一个用于名称,另一个用于url。我编辑了我的答案,希望它现在可以帮助! –

0

我想通了:

       try { 
           // response.getString("status"); 


            jarray = response.getJSONArray("companies"); 
            company_name=new String[jarray.length()];      
      for(int i=0;i<jarray .length();i++){ 
    JSONObject object = (JSONObject) jarray.get(i); 
    // Now get subcategory information as JSONObject 
    // JSONObject subcategoryObject =object .getJSONObject("subcategory"); 
    // Now you can get image and name from subcategoryObject 
    company_name[i]= object.getString("name2"); 
}