本文介绍了如何从指定的JSONObject属性值上的JSONArray中删除JSONObjects?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON响应,它有一个JSONArray(数据),它有一千个对象,每个对象有8个键/值对的值。



这是我的JSON回复







我想删除包含publishtype的所有JSONObject:UNPUBLISHED,而我的JSONArray只包含那些具有publishtype:PUBLISHED的对象。 br />


我正在使用org.json库中的类进行JSON解析。

这是我的代码。

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);

推荐答案

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
    }
}



希望它可以帮到你:)



-KR


Hope it helps you :)

-KR


这篇关于如何从指定的JSONObject属性值上的JSONArray中删除JSONObjects?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:31