2014-06-17 143 views
1

我是android新手,我試圖創建一個從JSON中捕獲數據的應用程序。 現在我已經獲得了所有數據,但問題是如何獲得我只需要的特定數據。Android從JSON獲取特定數據

這裏是我的JSON例子

[{"id":"152","category_id":"1","item_name":"Restaurant1","description":"Restaurant1","address":"Restaurant1","area":"42","area_name":"Kuta","longitude":"2131","latitude":"1231","open_detail":"24hours","kids":"","free_text_for_kids":"","cuisine_id":"3","cuisine_name":"Chinese","cuisine_uniqe_code":"CNS","price_category":"5","hotel_official_star_rating":"0","phone":"123","mobile_phone":"123","review":"Restaurant1","promotion":"0","promotion_free_text":"","promotion_start_date":"0000-00-00","promotion_end_date":"0000-00-00","active":"1","image_count":"3","created_date":"1401644001","created_by":"1","updated_date":"1402029631","updated_by":"1","facebook":"Restaurant1","twitter":"Restaurant1","instagram":"Restaurant1"}, 
{"id":"153","category_id":"1","item_name":"Restaurant2","description":"Restaurant2","address":"Restaurant2","area":"42","area_name":"Kuta","longitude":"1231","latitude":"1231231","open_detail":"24hours","kids":"","free_text_for_kids":"","cuisine_id":"17","cuisine_name":"Middle Eastern","cuisine_uniqe_code":"MID","price_category":"3","hotel_official_star_rating":"0","phone":"1231","mobile_phone":"231","review":"Restaurant2","promotion":"0","promotion_free_text":"","promotion_start_date":"0000-00-00","promotion_end_date":"0000-00-00","active":"1","image_count":"0","created_date":"1401644082","created_by":"1","updated_date":"1402029930","updated_by":"1","facebook":"Restaurant2","twitter":"Restaurant2","instagram":"Restaurant2"}, 
{"id":"162","category_id":"2","item_name":"Bars1","description":"Bars1","address":"Bars1","area":"34","area_name":"Seminyak","longitude":"213312","latitude":"21312","open_detail":"Bars1","kids":"","free_text_for_kids":"","cuisine_id":"","cuisine_name":null,"cuisine_uniqe_code":null,"price_category":"2","hotel_official_star_rating":"0","phone":"1231","mobile_phone":"1231","review":"Bars1","promotion":"0","promotion_free_text":"","promotion_start_date":"0000-00-00","promotion_end_date":"0000-00-00","active":"1","image_count":"0","created_date":"1401644461","created_by":"1","updated_date":"1402939937","updated_by":"1","facebook":"Bars1","twitter":"Bars1","instagram":"Bars1"}, 
{"id":"163","category_id":"2","item_name":"Bars2","description":"Bars2","address":"Bars2","area":"42","area_name":"Kuta","longitude":"1232131","latitude":"231","open_detail":"Bars2","kids":"","free_text_for_kids":"","cuisine_id":"","cuisine_name":null,"cuisine_uniqe_code":null,"price_category":"5","hotel_official_star_rating":"0","phone":"11231","mobile_phone":"213","review":"Bars2","promotion":"0","promotion_free_text":"","promotion_start_date":"0000-00-00","promotion_end_date":"0000-00-00","active":"1","image_count":"0","created_date":"1401644494","created_by":"1","updated_date":"1402030999","updated_by":"1","facebook":"Bars2","twitter":"Bars2","instagram":"Bars2"}, 

正如你可以看到,有兩個式CATEGORY_ID,我怎樣才能得到「1」只有CATEGORY_ID餐廳。 我知道我必須使用if語句,但我應該在哪裏放?

這裏是我的Java代碼

@Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 

       restaurant = new JSONArray(jsonStr); 


       // looping through All Contacts 
       for (int i = 0; i < restaurant.length(); i++) { 
        JSONObject c = restaurant.getJSONObject(i); 

        String item_name = c.getString(TAG_ITEM_NAME); 
        String cuisine_name = c.getString(TAG_CUISINE_NAME); 

        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_ITEM_NAME, item_name); 
        contact.put(TAG_CUISINE_NAME, cuisine_name); 


        // adding contact to contact list 
        restaurantList.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

感謝前:d

回答

1

創建的,如果來檢查,如果類別爲1,如果它再加入到HashMap如果它不那麼做的聲明不要繼續下一個json對象。

樣本:

for (int i = 0; i < restaurant.length(); i++) { 
     JSONObject c = restaurant.getJSONObject(i); 

     String category = c.getString("category_id"); 
     if(category.equals("1")) 
     { 
      String item_name = c.getString(TAG_ITEM_NAME); 
      String cuisine_name = c.getString(TAG_CUISINE_NAME); 

      // tmp hashmap for single contact 
      HashMap<String, String> contact = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      contact.put(TAG_ITEM_NAME, item_name); 
      contact.put(TAG_CUISINE_NAME, cuisine_name); 


      // adding contact to contact list 
      restaurantList.add(contact); 
     } 
    } 
+0

感謝您的回答,也許我可以使用它的另一種選擇:D – user3561934

+0

@ user3561934歡迎:))。如果你找到一個可以幫助你滿意的答案。 –

0

其實我自己找到了答案。 下面的代碼,我使用

for (int i = 0; i < restaurant.length(); i++) { 
    JSONObject c = restaurant.getJSONObject(i); 

    if(c.getString("category_id").equals("1")) { 

     String item_name = c.getString(TAG_ITEM_NAME); 
     String cuisine_name = c.getString(TAG_CUISINE_NAME); 

     // tmp hashmap for single contact 
     HashMap<String, String> contact = new HashMap<String, String>(); 

     // adding each child node to HashMap key => value 
     contact.put(TAG_ITEM_NAME, item_name); 
     contact.put(TAG_CUISINE_NAME, cuisine_name); 

     // adding contact to contact list 
     restaurantList.add(contact); 
    } 
} 

非常感謝大家,已經回答。我真的appriciate它:D