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

Rod_Algonquin

创建一个if语句,检查类别是否是1,如果,则将类别添加到if类别,否则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);
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JSON Path如何获取特定数据?

来自分类Dev

从以下JSON获取特定数据?

来自分类Dev

如何从JSON获取特定数据?

来自分类Dev

从 URL 获取 json 并显示特定数据

来自分类Dev

Json / Nodejs:如何从JSON文件获取特定数据

来自分类Dev

如何通过PHP从复杂的json获取特定数据

来自分类Dev

如何从JSON列表中获取特定数据

来自分类Dev

JSON解析C#获取特定数据

来自分类Dev

反序列化json并获取特定数据的方法

来自分类Dev

使用JavaScript从YQL JSON获取特定数据

来自分类Dev

如何使用 PHP 从 JSON 响应中获取特定数据

来自分类Dev

如何从嵌套的 JSON 中获取特定数据

来自分类Dev

仅从 Json 文件中获取特定数据

来自分类Dev

从XML获取特定数据

来自分类Dev

从URI获取特定数据

来自分类Dev

从html获取特定数据

来自分类Dev

从XML获取特定数据

来自分类Dev

从curl获取特定数据

来自分类Dev

从JSON提取特定数据

来自分类Dev

从json抓取特定数据

来自分类Dev

从JSON数据访问特定数据(Python)

来自分类Dev

Google脚本-从网站获取特定数据

来自分类Dev

如何从网址获取特定数据?

来自分类Dev

在xpath中获取特定数据

来自分类Dev

使用awk从ipconfig获取特定数据

来自分类Dev

从实例获取特定数据

来自分类Dev

Laravel模型获取特定数据

来自分类Dev

从数组对象(php)获取特定数据

来自分类Dev

从特定数量的行中获取数据