我需要在新行中返回json数组的每个元素。而是在一行中打印。我尝试使用'/ n'和空格,但它不工作。用新行加入json字符串值
String json = null;
//split cookie with delimiter to store in array
String wl[] = wishList.split("~");
//JSON RETURN VALUE
json = "[\"";
for(int i = 0; i < wl.length; ++i) {
json += wl[i];
//tried but didn't work
//json.split("\n");
}
json += "\"]";
System.out.println(json);
所以我试图cancatinating for循环,但它没有奏效。 我需要走出这样的
1.abc
2.bcd
3.efg
,但我得到这个输出
1.abcbcdefg
您是否阅读过['String#split'](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String))文档?只需使用'json + =“\ n”'或'json + = wl [i] +“\ n”'。你的问题的标题也让我相信你认为'json'是一个数组,它不是,它是'String'。 –
手动构建JSON是一种反模式。使用像杰克逊或Gson的图书馆 –