如何在目录中读取所有json文件的数据

时间:2018-10-14 12:08:36

标签: java json lambda java-8

我在目录中读取了一个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()); 
            } 
        } }

0 个答案:

没有答案