试图在java

时间:2016-12-10 05:04:05

标签: java json

我正在尝试从本地磁盘读取JSON文件,但我收到以下异常:

  

java.io.FileNotFoundException:F:\ test.json(系统找不到指定的文件)       at java.io.FileInputStream.open(Native Method)       在java.io.FileInputStream。(FileInputStream.java:146)       在java.io.FileInputStream。(FileInputStream.java:101)       在java.io.FileReader。(FileReader.java:58)       在main.resources.ReadJsonFile.main(ReadJsonFile.java:23)

我的代码:

JSONParser parser = new JSONParser();

try {
   Object obj = parser.parse(new FileReader("F:\\test.json"));
   JSONObject jsonObject = (JSONObject) obj;
   String name = (String) jsonObject.get("name");
   System.out.println(name);

   long age = (Long) jsonObject.get("age");
   System.out.println(age);

   // loop array
   JSONArray msg = (JSONArray) jsonObject.get("messages");
   Iterator<String> iterator = msg.iterator();
   while (iterator.hasNext()) {
      System.out.println(iterator.next());
   }

} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

0 个答案:

没有答案