使用gson解析json到java对象的问题 [英] Issue with parse json to java object using gson

查看:638
本文介绍了使用gson解析json到java对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是我想分析的数据的一个例子:

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

IsBean.java

  public interface IsBean extends Serializable,IsSerializable {

long getId();

$ b

User.java

  public class User实现IsBean,IsSerializable,Serializable {

String userName;
字符串密码;

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实现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);
尝试{
RequestObj request = gson.fromJson(jsonStr,RequestObj.class);
} catch(Exception e){
e.printStackTrace();





错误:


$ / / strong>


  java.lang.RuntimeException:无法为接口com.test.gson.shared调用无参数构造函数。 IsBean。用Gson注册一个InstanceCreator来解决这个问题。 
at com.google.gson.internal.ConstructorConstructor $ 8.construct(ConstructorConstructor.java:167)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:162 )
在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:93)
在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory。 java:172)
在com.google.gson.Gson.fromJson(Gson.java:795)
在com.google.gson.Gson.fromJson(Gson.java:761)
在com.google.gson.Gson.fromJson(Gson.java:710)
在com.google.gson.Gson.fromJson(Gson.java:682)
在com.test.gson.server .Test.main(Test.java:18)
导致:java.lang.reflect.InvocationTargetException $ b $在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)$ b $在sun.reflect。 NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.De leggingMethodAccessorImpl.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
引起:java.lang.InstantiationException :com.test.gson.shared.IsBean
at sun.misc.Unsafe.allocateInstance(Native Method)
... 14 more

解决方案

没有看到 CommunicationObject 的代码,我不能说当然,我猜测这个类有一个类型为 IsBean 的字段,用它来保存 User >。如果是这样,问题在于GSON扫描类 CommunicationObject 的对象 obj 的字段,并根据字段定义,该字段的值(键入 IsBean )必须被创建,因为该类型是一个接口,它不能为该字段的实例对象。 p>

换句话说,由于JSON字段没有指定对象类型,所以GSON必须依赖定义的字段类型来创建字段值的实例,这是无法完成的如果该类型是一个接口。



因此,如果对您有意义,请考虑更改该字段的类型。或着眼于创建 InstanceCreator IsBean (假设它在逻辑上是可能的)。



另请参阅:反序列化Gson中的抽象类



希望这有助于。


I want to parse json data into java object using google-gson.

Here is an example of the data I want to parse:

  {"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();
        }
    }
}

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. 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.

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.

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).

See also: Deserializing an abstract class in Gson

Hope this helps.

这篇关于使用gson解析json到java对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆