如标题所述,我想检查Java中外部Json文件的键。
我试过的
JSONObject json = new JSONObject("src/com/json/inventory.json");
System.out.println(json.keys());
问题:什么都不做。
还有什么其他可能性?
先感谢您!
编辑
Error
解决方案如下:
如果您使用的是this library,则首先应阅读文件内容,例如:
String unparsedJson = Files.readAllLines(Paths.get("src/com/json/inventory.json"))
.stream()
.reduce((a,b) -> a + b)
.get();
然后,您可以使用提供的代码解析json:
JSONObject json = new JSONObject(unparsedJson);
并使用以下方式迭代键:
json.keys().forEachRemaining(key -> {
System.out.println(key);
});