在JAVA中使用gson从/到Json读取/写入不同对象的数组列表
我需要保存并加载Json文件,其中包含一个ArrayList对象。 数组列表中的对象:单项式多项式和ComplexFunction 说明: 在这个项目中,我有3个主要对象和1个对象,它们将所有对象保存在一个列表中。 (所有这些实现了“功能”接口)在JAVA中使用gson从/到Json读取/写入不同对象的数组列表,java,json,oop,gson,Java,Json,Oop,Gson,我需要保存并加载Json文件,其中包含一个ArrayList对象。 数组列表中的对象:单项式多项式和ComplexFunction 说明: 在这个项目中,我有3个主要对象和1个对象,它们将所有对象保存在一个列表中。 (所有这些实现了“功能”接口) 单体-从两个字段构建双系数,整数幂 多项式-单项式的数组列表 ComplexFunction—保存两个函数对象(左侧和右侧)以及它们之间的操作(time div…) 我需要再构建一个对象,Functions\uGUI,它可以在一个集合中包含所有这些“函
public void saveToFile(String file) throws IOException {
try
{
Gson gson = new Gson();
String json = gson.toJson(this);
System.out.println(json);
String file_name = file+".json";
PrintWriter pw = new PrintWriter(new File(file_name));
pw.write(json);
pw.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
public void initFromFile(String file) throws IOException {
try {
Gson gson = new Gson();
JsonReader reader = new JsonReader (new FileReader(file));
Functions_GUI tg1 = gson.fromJson(reader, Functions_GUI.class);
this.list=tg1.list;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
完成此操作后,我将获得此文件:
[
{"PloynomList":[]},
{"CF_LeftFunction":
{"PloynomList":[
{"Coefficient":1.0,"Power":3},
{"Coefficient":-13.0,"Power":2},
{"Coefficient":-2.0,"Power":0}]},
"CF_RightFunction":{
"PloynomList":[
{"Coefficient":1.0,"Power":1},
{"Coefficient":-2.0,"Power":0}]},
"CF_Operation":"Divid"},
{"PloynomList":[
{"Coefficient":1.0,"Power":3},
{"Coefficient":-13.0,"Power":2},
{"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
{"Coefficient":1.0,"Power":4},
{"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
{"Coefficient":1.0,"Power":1},
{"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
{"Coefficient":12.0,"Power":1},
{"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
{"Coefficient":-2.0,"Power":7},
{"Coefficient":1.0,"Power":1}]}
]
public void saveToFile(String file) throws IOException {
try
{
Gson gson = new Gson();
String json = gson.toJson(this);
System.out.println(json);
String file_name = file+".json";
PrintWriter pw = new PrintWriter(new File(file_name));
pw.write(json);
pw.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
public void initFromFile(String file) throws IOException {
try {
Gson gson = new Gson();
JsonReader reader = new JsonReader (new FileReader(file));
Functions_GUI tg1 = gson.fromJson(reader, Functions_GUI.class);
this.list=tg1.list;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
您可以看到,上面的文件是使用所有对象的所有字段创建的。。
但是,当我尝试读取同一文件并从中创建新函数\u GUI时,会出现以下错误:
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable to invoke no-args constructor for interface Ex1.function. Register an InstanceCreator with Gson for this type may fix this problem.
at Ex1.Functions_GUI.initFromFile(Functions_GUI.java:125)
at Ex1.MainTest.main(MainTest.java:183)
Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface Ex1.function. 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.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:795)
at Ex1.Functions_GUI.initFromFile(Functions_GUI.java:122)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:164)
... 7 more
Caused by: java.lang.InstantiationException: Ex1.function
at sun.misc.Unsafe.allocateInstance(Native Method)
... 13 more
请告知的可能重复。您能展示一下您的
POJO
类吗。看起来,Gson
不知道在发现接口时使用哪个实现。