java 兼容JSONArray remove低版本

由于考虑到JSONArray 的低版本兼容问题,通过逻辑代码仿造JSONArray的remove实现

JSONArray的remove方法是在API level 19时加入的,在低版本调用时会出现错误

 /**
     * 处理低版本的json库
     * @param jsonArray
     * @param index
     * @return
     */
 private JSONArray remove(JSONArray jsonArray,int index){
        JSONArray mJsonArray  = new JSONArray();
        if(index < 0)
            return mJsonArray;

        if(index > jsonArray.length())
            return mJsonArray;

        for( int i = 0; i < index; i++){
            try {
                mJsonArray.put(jsonArray.getJSONObject(i));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        for( int i=index+1;i< jsonArray.length();i++){
            try {
                mJsonArray.put(jsonArray.getJSONObject(i));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return mJsonArray;
    }

猜你喜欢

转载自blog.csdn.net/wang852575989/article/details/88232922
今日推荐