java: read json file

https://commons.apache.org/proper/commons-io/download_io.cgi
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
https://stleary.github.io/JSON-java/org/json/JSONObject.html
https://github.com/stleary/JSON-java

 

HttpClient 5.1.2
https://hc.apache.org/downloads.cgi

 

 

 try {
                String filename = "geovindu.json";
                InputStream is = SqlDuMain.class.getResourceAsStream(filename);
                JSONTokener tokener = new JSONTokener(is);
                JSONObject object = new JSONObject(tokener);
                System.out.println("Id  : " + object.getLong("id"));
                System.out.println("Name: " + object.getString("name"));
                System.out.println("Age : " + object.getInt("age"));

                System.out.println("Courses: ");
                JSONArray courses = object.getJSONArray("courses");
                for (int dui = 0; dui < courses.length(); dui++) {
                    System.out.println("  - " + courses.get(dui));
                }

               /*Geovin Du*/ //2
                File file=new File("src/geovindu.json");
                if (file.exists()) {

                    String content = FileUtils.readFileToString(file, "UTF-8");

                    System.out.println(content);
                    //String filename1 = "geovindu.json";
                    JSONObject jsonObject = parseJSONFile("src/geovindu.json");
                    System.out.println(jsonObject.toString());

                }




            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }



        }
        catch (NoSuchMethodException exception)
        {
            exception.printStackTrace();
        }
        catch (Exception exception){
            exception.printStackTrace();
        }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章