使用 gson 将 json 解析为 java 对象的问题

2023-11-21

我想使用将 json 数据解析为 java 对象谷歌-gson.

这是我要解析的数据的示例:

  {"isBean":{"userName":"test","password":"password112"}}

IsBean.java

public interface IsBean extends Serializable,IsSerializable{

    long getId();

}

用户.java

public class User implements IsBean,IsSerializable,Serializable {

    String userName;
    String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public long getId() {
        return 0;
    }


}

RequestObj.java

public class RequestObj implements IsBean, Serializable,IsSerializable{

    IsBean  isBean;
    @Override
    public long getId() {
        return 0;
    }
     public IsBean getIsBean() {
        return isBean;
    }
    public void setIsBean(IsBean isBean) {
        this.isBean = isBean;
    }
}

测试.java

public class Test {

    public static void main(String[] args) {
        Gson gson = new Gson();
        User user=new User();
        user.setPassword("password112");
        user.setUserName("test");
        RequestObj requestObj=new RequestObj();
        requestObj.setIsBean(user);
        String jsonStr=gson.toJson(requestObj);
        System.out.println("jsonStr--->"+jsonStr);
        try{
            RequestObj request=gson.fromJson(jsonStr, RequestObj.class);
        }catch (Exception e) {
        e.printStackTrace();
        }
    }
}

Error:

java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.test.gson.shared.IsBean. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:167)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:761)
    at com.google.gson.Gson.fromJson(Gson.java:710)
    at com.google.gson.Gson.fromJson(Gson.java:682)
    at com.test.gson.server.Test.main(Test.java:18)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:164)
    ... 8 more
Caused by: java.lang.InstantiationException: com.test.gson.shared.IsBean
    at sun.misc.Unsafe.allocateInstance(Native Method)
    ... 14 more

没有看到代码CommunicationObject,我不能肯定地说,但我有信心猜测该类有一个类型字段IsBean您用它来容纳User。如果是这样,问题是GSON扫描对象的字段obj班级的CommunicationObject并根据字段定义,字段的值(键入IsBean)必须创建,但由于该类型是一个接口,因此它不能该字段的实例对象。

换句话说,由于 JSON 字段没有指定对象类型,GSON 必须依赖字段定义的类型来为字段值创建实例,如果类型是接口,则无法完成此操作。

因此,如果这对您有意义,请考虑更改该字段的类型。或者考虑创建InstanceCreator of IsBean(怀疑这在逻辑上是可能的)。

也可以看看:在 Gson 中反序列化抽象类

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 gson 将 json 解析为 java 对象的问题 的相关文章

随机推荐