我在目录中读取了一个json文件。我想读取所有json文件,并且想对该目录应用watchservice。如果在java8中可以,
Object obj = new JSONParser().parse(new FileReader("C://json//jsonexample.json"));
JSONObject jo = (JSONObject) obj;
String firstName = (String) jo.get("firstName");
String lastName = (String) jo.get("lastName");
System.out.println(firstName);
System.out.println(lastName);
long age = (long) jo.get("age");
System.out.println(age);
Map address = ((Map)jo.get("address"));
Iterator<Map.Entry> itr1 = address.entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
System.out.println(pair.getKey() + " : " + pair.getValue());
}
JSONArray ja = (JSONArray) jo.get("phoneNumbers");
Iterator itr2 = ja.iterator();
while (itr2.hasNext())
{
itr1 = ((Map) itr2.next()).entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
System.out.println(pair.getKey() + " : " + pair.getValue());
}
} }