2015-09-07 106 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"); 
}