【问题标题】:Issue with parse json to java object using gson使用 gson 将 json 解析为 java 对象的问题
【发布时间】:2012-12-14 06:38:50
【问题描述】:

我想使用google-gson 将 json 数据解析为 java 对象。

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

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

IsBean.java

public interface IsBean extends Serializable,IsSerializable{

    long getId();

}

User.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;
    }
}

Test.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();
        }
    }
}

错误:

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

【问题讨论】:

    标签: java gwt gson


    【解决方案1】:

    没有看到CommunicationObject 的代码,我不能肯定地说,但我有信心地猜测该类有一个IsBean 类型的字段,您可以在其中使用它来保存User。如果是这样,问题是 GSON 扫描类 CommunicationObject 的对象 obj 的字段,并且根据字段定义,必须创建字段的值(键入 IsBean),但是因为type 是一个接口,它不能为该字段实例化对象。

    也就是说,由于JSON字段没有指定对象类型,GSON必须依赖定义的字段类型来为字段值创建实例,如果类型是接口则无法做到。

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

    另见:Deserializing an abstract class in Gson

    希望这会有所帮助。

    【讨论】:

    • 正如我所怀疑的那样。不幸的是,接口(抽象类)字段将是创建其实例的默认方式。所以你必须决定IsBean的默认类型是什么。
    【解决方案2】:

    借助 Stackoverflow Answer 解决了问题。

    创建 gson 对象:

    Gson gson = new GsonBuilder().registerTypeAdapter(IsBean.class, new InterfaceAdapter<IsBean>()).create();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多