简体   繁体   English

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

[英]Issue with parse json to java object using gson

I want to parse json data into java object using google-gson . 我想使用google-gson将json数据解析为java对象。

Here is an example of the data I want to parse: 以下是我要解析的数据示例:

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

IsBean.java IsBean.java

public interface IsBean extends Serializable,IsSerializable{

    long getId();

}

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

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

Without seeing the code of CommunicationObject , I can't say for sure BUT I am guessing with confidence that the class has a field of type IsBean in which you use it to hold the User . 没有看到CommunicationObject的代码,我不能肯定地说,但我充满信心地猜测该类有一个类型为IsBean的字段,您可以在其中使用它来保存User If so, the problem is that GSON scans fields of the object obj of the class CommunicationObject and based on the field definition, the value of the field (which is typed IsBean ) will have to be created BUT since the type is an interface, it can't instance object for that field. 如果是这样,问题是GSON扫描类CommunicationObject的对象obj的字段并且基于字段定义,必须创建字段的值(类型为IsBean ),因为类型是接口,它不能为该字段的实例对象。

Another words, as JSON field does not specifies the object type, GSON must relies on the defined type of the field to create the instance for the field value which can't be done if the type is an interface. 换句话说,由于JSON字段未指定对象类型,因此GSON必须依赖于字段的已定义类型来为字段值创建实例,如果类型是接口,则无法执行该实例。

So, consider changing the type of that field if that make sense to you. 因此,如果对您有意义,请考虑更改该字段的类型。 OR look into creating InstanceCreator of IsBean (doute that it is logically possible). 或者研究创建IsBean InstanceCreator (doute,它在逻辑上是可行的)。

See also: Deserializing an abstract class in Gson 另请参阅: 在Gson中反序列化抽象类

Hope this helps. 希望这可以帮助。

The problem is solved With the help of Stackoverflow Answer . 在Stackoverflow Answer的帮助下解决了这个问题。

Create gson object: 创建gson对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM