使用gson解析json到java对象的问题(Issue with parse json to java object using gson)

编程入门 行业动态 更新时间:2024-10-25 00:26:47
使用gson解析json到java对象的问题(Issue with parse json to java object using gson)

我想使用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

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

最满意答案

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

换句话说,因为JSON字段没有指定对象类型,所以GSON必须依赖定义的字段类型来为字段值创建实例,如果类型是接口,则不能完成。

所以,如果这对您有意义,请考虑更改该字段的类型。 或者研究创建IsBean InstanceCreator (认为​​它在逻辑上是可行的)。

另请参见: 在Gson中反序列化抽象类

希望这可以帮助。

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.

更多推荐

本文发布于:2023-08-02 19:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   java   json   gson   parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!