繁体   English   中英

Android从JSON获取特定数据

[英]Android get specific data from 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

创建一个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);
        }
    }

实际上,我自己找到了答案。 这是我使用的代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM