Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a JSON Response that has a JSONArray (“data”), it has thousand objects, and each object has 8 Key/Value pair’s values.

Here is my JSON Response

http://pastebin.com/qtgjTv8B

I want to remove all the JSONObject that contains "publishtype": "UNPUBLISHED” and my JSONArray only contains those objects that have "publishtype": "PUBLISHED".

I am using classes from org.json library for JSON Parsing.
Here is my code.
RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest req = new StringRequest(JsonUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
    
                            for (int i = 0; i < jsonArray.length(); i++) {
                                // Here i am getting the all JSONObjects from JSONArray,
//how can i filter the JSONObjects that
								// only having "publishtype": "PUBLISHED".
                                JSONObject jsonObjectFromArray = jsonArray.getJSONObject(i);
								                         
                            }
   
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(req);
Posted
Updated 11-Oct-15 23:41pm
v2

1 solution

Java
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject obj= jsonArray.getJSONObject(i);
    if(obj.getString("publishtype").equals("PUBLISHED"))
    {
        // add this item in some collection i.e PublishedList, and later use this collection
    }
}

Hope it helps you :)

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900