String web_inf = getServletContext().getRealPath("/WEB-INF/");
String location = web_inf + "\\config.csv";
System.out.println("这是Servlet");
System.out.println("~~~~~~~~~~~~~~~~~~~");
ReadingAndWriting read = new ReadingAndWriting();
String json = "{\"";
String str = read.fileread(location);
String[] str1 = str.split("\r\n");
for(int i=0;i<=str1.length;i++){
String str2[] = str1[i].split(",");
json += str2[1] + "\":\"" + str2[2] + "\",\"";
}
json += "\"}";
System.out.println(json);
这段代码中json字符串在for循环后就输出不出来了,在for循环里输出是有数据的
------解决思路----------------------
for(int?i=0;i<=str1.length;i++){
????????????String?str2[]?=?str1[i].split(",");
????????????json?+=?str2[1]?+?"\":\""?+?str2[2]?+?"\",\"";
????}
其中 int?i=0;i<=str1.length;i++ i<= str1.length 最后一个数组越界了 应该 会报错吧。。
改成 for(int?i=0;i<str1.length;i++){ 试试