下面是我的代码示例
public record Person(String firstName, String lastName) {}
@Test
void test() {
String json =
"""
{
"firstName": "John",
"lastName": "Doe"
}
""";
Person person = new Gson().fromJson(json, Person.class);
assertEquals("John", person.firstName);
}
以下是我得到的一个例外:
java.lang.AssertionError: AssertionError (GSON 2.8.9): java.lang.IllegalAccessException: Can not set final java.lang.String field com.test.GsonTest$Person.firstName to java.lang.String
是否有任何方法使用Gson将JSON反序列化为java记录?
发布于 2022-11-22 21:06:30
GSON2.10支持Java 16+记录:https://github.com/google/gson/releases
如果不能升级,以前链接的https://github.com/google/gson/issues/1794在线程中有很多解决方案。
https://stackoverflow.com/questions/73909212
复制相似问题