Reputation: 49
I'm newbie on android and I'm trying to create an application that catch data from JSON. For now I already get all data, but the problem is how can I get specific data that I only need.
Here's my JSON example
[{"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"},
As you can see, there's two type category_id, how can I get only category_id "1" for restaurant. I know that I have to use if statement, but where should I put it?
Here's My java code
@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;
}
Thanks Before :D
Upvotes: 2
Views: 1436
Reputation: 49
Actually I found the answer by myself. Here's the code that I use
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);
}
}
Thanks a lot for everyone that already answer it. I really appriciate it :D
Upvotes: 0
Reputation: 26198
Create an if statement that checks if the category is 1
and if it is then add the to the HashMap
if it is not then do not do anything continue to the next json object.
sample:
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);
}
}
Upvotes: 2