需要删除java中的最后一个逗号

时间:2016-07-04 09:37:58

标签: java

这是我的代码

jsonUIResponse.append("[");
    String finalStr1 = null;
    String finalStr2 = null;
    Iterator itr_1 = skus.iterator();
    while (itr_1.hasNext()){
        jsonUIResponse.append("{");
        //System.out.println("SKU1 in loop :::::: " + itr_1.next());
        String str = itr_1.next().toString();
        String str1 = str.substring(1, (str.length()-1));
        String[] str2 = str1.split(", ");
        for(int i=0;i<str2.length;i++){
            String[] str3 = str2[i].split("=");
            for(int j=0;j<str3.length;j++){
            }
            finalStr1 = str3[0] ;
            finalStr2 = str3[1] ;
            System.out.println("entry2 :: " + finalStr1);
            System.out.println("entry3 :: " + finalStr2);

            if (finalStr1.equals("value")) {                        
            jsonUIResponse.append("\"value\":\"" + finalStr2 + "\",");
            }else if (finalStr1.equals("sequence")) {
                jsonUIResponse.append("\"sequence\":\"" + finalStr2 + "\",");   
            }else if (finalStr1.equals("availableinventory")) {
                jsonUIResponse.append("\"availableinventory\":\"" + finalStr2 + "\","); 
            }else if (finalStr1.equals("skuid")) {
                jsonUIResponse.append("\"skuid\":\"" + finalStr2 + "\",");  
            }
            else if (finalStr1.equals("partnumber")) {
                jsonUIResponse.append("\"partnumber\":\"" + finalStr2 + "\","); 
            }
        }
        jsonUIResponse.append("\"defining\":\"size\"");
        jsonUIResponse.append("},");

    }
    System.out.println("entryresp3 :: " + jsonUIResponse);
                jsonUIResponse.append("]");
    jsonUIResponse.append("},");

它会输出如下:

[ {"value":"4","sequence":"1","availableinventory":"10","skuid":"24325365","partnumber":"602016000951676","defining":"size"}, {"value":"6","sequence":"2","availableinventory":"10","skuid":"24325371","partnumber":"602016000951677","defining":"size"}, {"value":"8","sequence":"3","availableinventory":"10","skuid":"24325377","partnumber":"602016000951678","defining":"size"}, {"value":"10","sequence":"4","availableinventory":"10","skuid":"24325346","partnumber":"602016000951679","defining":"size"}, {"value":"12","sequence":"5","availableinventory":"10","skuid":"24325349","partnumber":"602016000951680","defining":"size"}, {"value":"14","sequence":"6","availableinventory":"10","skuid":"24325353","partnumber":"602016000951681","defining":"size"}, {"value":"16","sequence":"7","availableinventory":"10","skuid":"24325359","partnumber":"602016000951682","defining":"size"***},***]}]},

必须删除上面突出显示的(粗体,斜体)逗号。请帮忙解决这个问题

2 个答案:

答案 0 :(得分:1)

通常最好使用JSON库来构造对象。 在这种情况下,您可以这样做:

if (itr_1.hasNext()) {
    jsonUIResponse.append("},");
} else {
    jsonUIResponse.append("}");
}

答案 1 :(得分:0)

我的解决方案是添加变量计数。在while循环之外添加此声明

int count = 0;

将此statemet jsonUIResponse.append(“{”); 替换为以下语句

if(count == 0){
 //for the first element, you only need to add {
 jsonUIResponse.append("{");
}
else{
 jsonUIResponse.append(",{");
}

然后用这些替换 jsonUIResponse.append(“},”);

jsonUIResponse.append("}");
count = 1;