我的代码创建一个新的
JSONObject并写入文件:
- JSONObject obj = new JSONObject();
- obj.put("name","abcd");
- obj.put("age",new Integer(100));
- JSONArray list = new JSONArray();
- list.add("msg 1");
- list.add("msg 2");
- list.add("msg 3");
- obj.put("messages",list);
- try {
- FileWriter file = new FileWriter("c:\\test.json");
- file.write(obj.toJSONString());
- file.flush();
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.print(obj);
我的问题在于
- file.write(obj.toJSONString());
它说
The method toJSONString() is undefined for the type JSONObject.
我错过了任何图书馆吗?或者我错了吗?有替代方法吗?