java代码读取统计目录下的json文件并转换成json对象

    @Test
    void contextLoads() throws Exception {

        String fileName = "1.json";
        String currentFilePath = new Object(){}.getClass().getEnclosingClass().getResource("").getPath()+"/"+fileName;
        String substring = currentFilePath.replaceAll("target/test-classes", "src/test/java").substring(1);
        // 使用相对路径指定JSON文件,假设在同一目录下
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(new File(substring));
        System.out.println("jsonNode = " + jsonNode);
    }

你可能感兴趣的:(java,json,开发语言)