How do I remove a specific element from a JSONArray?

后端 未结 9 1742
执笔经年
执笔经年 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:55

    You can use reflection

    A Chinese website provides a relevant solution: http://blog.csdn.net/peihang1354092549/article/details/41957369
    If you don't understand Chinese, please try to read it with the translation software.

    He provides this code for the old version:

    public void JSONArray_remove(int index, JSONArray JSONArrayObject) throws Exception{
        if(index < 0)
            return;
        Field valuesField=JSONArray.class.getDeclaredField("values");
        valuesField.setAccessible(true);
        List values=(List)valuesField.get(JSONArrayObject);
        if(index >= values.size())
            return;
        values.remove(index);
    }
    
        

    提交回复
    热议问题