去除java list 中的 [] 括號


項目中經常遇到

java 后台 list中的數據要通過json 傳入到前台頁面中

這時 就需要去除 list中給我加上的 [] 。

方法如下

1.重寫 AbstractCollection類的toString 方法

private String toString(Collection collection){
        StringBuffer stringbuffer = new StringBuffer();
        Iterator iterator1 = collection.iterator();
        boolean flag = iterator1.hasNext();
        do
        {
            if(!flag)
                break;
            Object obj = iterator1.next();
            stringbuffer.append(obj != this ? String.valueOf(obj) : "(this Collection)");
            flag = iterator1.hasNext();
            if(flag)
                stringbuffer.append(",");
        } while(true);
        return stringbuffer.toString();
    }

2.或者使用 org.apache.commons.lang.StringUtils 

StringUtils.strip( "['a','b','c','d']", "[]"); 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM