根据id获取json文件中的值

 public static String getEmail(String id){
        BufferedReader reader = null;
        String laststr = "";
        String email = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(
                    PctJsonUtil.class.getResource("/").getFile().toString() + "/email.json");

            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            JSONObject jsoncode = JSONObject.parseObject(laststr);
            email = String.valueOf(jsoncode.get(id));

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return email;
    }

测试json字符串: {“EMAIL”:”值”,”myEmailAccount”:”值”,”myEmailPassword”:”值”}

你可能感兴趣的:(JavaSE,json,获取json)