How do I remove a specific element from a JSONArray?

后端 未结 9 1752
执笔经年
执笔经年 2020-11-29 08:18

I am building one app in which I request a PHP file from server. This PHP file returns a JSONArray having JSONObjects as its elements e.g.,

[ 
  {
           


        
9条回答
  •  悲&欢浪女
    2020-11-29 08:45

    We can use iterator to filter out the array entries instead of creating a new  Array. 
    
    'public static void removeNullsFrom(JSONArray array) throws JSONException {
                    if (array != null) {
                        Iterator iterator = array.iterator();
                        while (iterator.hasNext()) {
                            Object o = iterator.next();
                            if (o == null || o == JSONObject.NULL) {
                                iterator.remove();
                            }
                        }
                    }
                }'
    
        

    提交回复
    热议问题