the problem is the method you are using is not sufficient.
one way of doing this, is to use another type of array (e.g. ArrayList) then Override its toString method.
here is an example:
public static void main(String... args) throws Exception{
ArrayList<String> array=new ArrayList<String>(){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean isFirst=true;
for(String s : this){
if(!isFirst){
sb.append(",");
}
isFirst=false;
sb.append("\""+s+"\"");
}
sb.append("]");
return sb.toString();
}
};
for (int i = 0; i < 5; i++) {
array.add("someproduct");
}
System.out.println(array);
}
output:
["someproduct","someproduct","someproduct","someproduct","someproduct"]
NOTE:
There are many ways to make this code reusable, e.g.
- creating a new class
ArrayListWithNicePrint that extends ArrayList
- creating a
static method that returns you a fresh ArrayList with nice Print (I prefer this one really)
EDIT: (Based ON comment):
public class ArrayListWithNicePrint<E> extends ArrayList{
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean isFirst=true;
for(Object s : this){
if(!isFirst){
sb.append(",");
}
isFirst=false;
sb.append("\""+s.toString()+"\"");
}
sb.append("]");
return sb.toString();
}
}
Testing:
public class Testing{
public static void main(String... args){
ArrayListWithNicePrint<String> list= new ArrayListWithNicePrint<String>();
list.add("hi");
list.add("hello");
System.out.println(list);
}
}
Arrays.toString(Id)doesn't produce the output you desire, simply loop over the array and build that output yourself.writeris, but doeswriter.key("product").value(Id);give the output you seek?