【发布时间】:2015-07-30 15:48:28
【问题描述】:
我想用 java 写一个 json 文件,但是它不起作用,我收到这个警告: 我想知道如何做到这一点,因为我要将一个选项卡式的 cfg 文件转换为 json。
Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList<E> should be parameterized
我有这个代码:
package json;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonWriter {
public static void main(String[] args) {
JSONObject countryObj = new JSONObject();
countryObj.put("Name", "India");
countryObj.put("Population", new Integer(1000000));
JSONArray listOfStates = new JSONArray();
listOfStates.add("Madhya Pradesh");
listOfStates.add("Maharastra");
listOfStates.add("Rajasthan");
countryObj.put("States", listOfStates);
try {
// Writing to a file
File file=new File("JsonFile.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
System.out.println("Writing JSON object to file");
System.out.println("-----------------------");
System.out.print(countryObj);
fileWriter.write(countryObj.toJSONString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
请描述预期输出和实际输出。
-
IIRC 这个 JSON 库太老了,不支持泛型,这就是你收到这个警告的原因。你可以忽略它。
-
你的例子工作得很好,类型安全警告指的是stackoverflow.com/a/16415553/1853785