gson:illegalstateexception:应为begin\u对象,但为string

krugob8w  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(232)

我正在尝试读取.txt文件并使用gson将其转换为target对象

2.Operation;
3.Amount;
4.Item_of_expenditure;
5.Balance;

这是我的密码:

return () -> {
        ReadFile readFile;
        String fileStringContent = asString(resourceFile);
        readFile = gson.fromJson(fileStringContent, ReadFile.class);
        return readFile;
    };

 private String asString(Resource resource) {
    try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
        return FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

但我收到一个例外:

IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $

考虑到目标类中的第一个字段具有字符串类型,这很奇怪:

public class ReadFile {

     private String operation;
     private Double amount;
     private String itemOfExpenditure;
     private Double oldBalance;
  }

附笔。:

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题