'How to get specific data from JSON OR URL in Android? [closed]

I want to get particular user data from PHP MySql in Android. Here is my URL (xxx//xxxxxxx/client_vendor_mgmt/subcategory.php?cat_id=2) and here is the URL data in json:

[
    {
        "subcat_id": "1",
        "subcat_code": "cell1",
        "subcat_name": "cell",
        "cat_id": "2",
        "subcat_created_date": "0000-00-00",
        "subcat_isactive": "Y",
        "subcat_updated_date": "0000-00-00"
    },
    {
        "subcat_id": "2",
        "subcat_code": "cell2",
        "subcat_name": "tv",
        "cat_id": "2",
        "subcat_created_date": "0000-00-00",
        "subcat_isactive": "Y",
        "subcat_updated_date": "0000-00-00"
    }
]

I want to get all the record of "cat_id":"2". How can I get this record?



Solution 1:[1]

Here is the code

try {
            JSONArray array=new JSONArray("yourJsonString");

            JSONArray sortedArray = new JSONArray();
            for(int i=0;i<array.length();i++)
            {
                JSONObject obj=array.getJSONObject(i);
                if(obj.getString("cat_id").equalsIgnoreCase("2"))
                {
                    sortedArray.put(obj);
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Solution 2:[2]

Use a json Parser Library like Gson and Jackson.

Solution 3:[3]

        JSONArray ja=new JSONArray("yourResponsString");
        JSONObject jo=ja.getJSONObject(0);
        String cat_id=jo.getString("cat_id");
        if(cat_id.equals("2")){

            // this is your object
        }

Solution 4:[4]

 JSONArray ja=new JSONArray("yourResponsString");
    for(int i=0;i<ja.length;i++)
{
JSONObject obj = ja.getJSONObject(i);
    String cat_id=obj.getString("cat_id");
    if(cat_id.equals("2")){

        // this is your object
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohan Pawar
Solution 2 SerCna
Solution 3 NavinRaj Pandey
Solution 4 Ramesh Devaiya