【发布时间】:2021-12-09 00:45:58
【问题描述】:
我认为这很简单,但它不断在InvocationTargetException reader.hasNext() 崩溃。
File jsonInputFile = new File(root + "/dicts/phrases.json");
JsonReader reader = new JsonReader(new FileReader(jsonInputFile));
while (reader.hasNext()) {
System.out.println(reader.nextName());
}
这到底有什么问题?尽管 json 文件只有一行,while 循环会一直持续下去。
【问题讨论】:
-
....你能在 reader.hasNext() 之前关闭一个阅读器吗??
-
@Susan Mustafa 删除了
reader.close();,但它并没有改变任何东西。我仍然在同一行上得到相同的InvocationTargetException错误。 while 循环是无限的,任何对reader.nextName()的调用都会引发错误。 -
您能粘贴您的 json 的一部分吗?您是否尝试过使用简单的 json...仅包含 1 个元素来查看是否是可能导致您出现问题的数据?
-
detailed information about the error 那么你能分享一下错误的细节吗?也许更容易理解问题的原因。
-
@BroscR
Caused by: java.lang.IllegalStateException: Expected a name but was @BEGIN_OBJECT at line 1 column 2 path $ at com.google.gson@2.8.8/com.google.gson.stream.JsonReader.nextName(JsonReader.java:788)
标签: java json jsonreader