如何使用 gson 将此对象序列化和反序列化为 json:
public class test{
@Expose
public List< Pair<Double,Double> > list;
@Expose
public int alpha;
}
我已经试过了:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String str = gson.toJson(testInstance,test.class);
其中 testInstance 是类 test 的实例,但由于 List 中的 Pair 结构,它无法正常工作。
最佳答案
您已经使用 excludeFieldsWithoutExposeAnnotation
方法配置了 Gson
对象。
来自 documentation :
Configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation.
你能改变 Pair 类吗?如果是,则必须向要序列化为 JSON 的每个属性添加 @Expose
注释。如果没有,您可以在您的程序中创建类似的类并添加注释。
关于java - 使用 GSON 序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17750372/