Android从JSON获取特定数据

时间:2014-06-17 21:47:54

标签: java 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,我怎样才能获得餐厅的category_id“1”。 我知道我必须使用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

2 个答案:

答案 0 :(得分:1)

创建一个if语句,检查类别是否为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);
        }
    }

答案 1 :(得分: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);
    }
}

非常感谢已经回复的每个人。我真的很喜欢它:D