JSON替换'。'同

时间:2015-08-07 17:17:48

标签: java json replace

我想要一个将JSON文件作为输入和结果JSON文件的程序。输入文件包含:

{"empname":"surname.firstname","department.name":"production","salary":11254.42}

输出文件必须用输入JSON的'_'(下划线)替换'。'(点)。 期待的输出:

{"empname":"surname_firstname","department_name":"production","salary":11254_42}

我希望这个程序使用JAVA,而不使用序列化和反序列化。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是Java 7 +:

String str = new String(Files.readAllBytes(Paths.get("in.json")), StandardCharsets.UTF_8)
        .replace('.', '_');

Files.write(Paths.get("out.json"), str.getBytes("UTF-8"), StandardOpenOption.WRITE);