如何使用Gson将对象转换为String

时间:2018-01-02 18:59:29

标签: android gson

我已经使用Gson(版本2.6.2)近两年了。但是从最近几天开始,当我尝试将对象转换为String时,我得到了以下错误。

Fatal Exception: java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible

我有这堂课:

public class VisitReportModel implements SyncableEntity
{
    private final String LOG_TAG=VisitReportModel.class.getSimpleName();

    private Long Id;
    private Date VisitDate;
    private Date NextVisitDate;
    private String AdvisorId;
    private String ProducerId;
    private Date LastUpdate;
    private List<FieldModel> Fields;
    private Long CropTypeId;
    private String Observations;
    private Boolean Deleted;
    private Integer Year;

    // Local id for searching after upload and set the id received
    private Long LocalId;

    public VisitReportModel()
    {
    }

    public VisitReportModel(VisitReportModel visitReport)
    {
        List<FieldModel> fieldModels = visitReport.getFieldModels();
        Fields = fieldModels;

        setId(visitReport.getId());
        setDeleted(visitReport.getDeleted());
        setVisitDate(visitReport.getVisitDate());
        setNextVisitDate(visitReport.getNextVisitDate());
        setAdvisorId(visitReport.getAdvisorId());
        setProducerId(visitReport.getProducerId());
        setLastUpdate(visitReport.getLastUpdate());
        setLocalId(visitReport.getLocalId());
        setCropTypeId(visitReport.getCropTypeId());
        setObservations(visitReport.getObservations());
        setYear(visitReport.getYear());
    }
}

(我排除了所有的吸气剂和制定者)。

当我尝试将此VisitReportModel对象转换为String(特别是在第二行)时,会发生错误:

Gson gson = new Gson();
String jsonObjectInString = gson.toJson(visitReport);

但奇怪的是,我已经做了很长时间了,现在发生了这个错误,我真的不知道为什么。有人有线索吗?谢谢!

3 个答案:

答案 0 :(得分:0)

我不知道为什么会出现这种错误,我还在搜索。但我找到了一个对我有用的解决方案。

GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC);
Gson gson = builder.create();

希望它有效!!!

答案 1 :(得分:0)

您是否在应用中启用了proguard,然后尝试以这种方式序列化/反序列化:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class VisitReportModel implements SyncableEntity
{
    private final String LOG_TAG=VisitReportModel.class.getSimpleName();

    @SerializedName("id")
    @Expose
    private Long Id;

    //............
}

答案 2 :(得分:0)

因此,通过网络搜索我意识到我必须将@Expose注释添加到我想要序列化/反序列化的所有字段中。所以我的对象就这样结束了:

public class VisitReportModel implements SyncableEntity
{
    private final String LOG_TAG=VisitReportModel.class.getSimpleName();
    @Expose
    private Long Id;
    @Expose
    private Date VisitDate;
    @Expose
    private Date NextVisitDate;
    @Expose
    private String AdvisorId;
    @Expose
    private String ProducerId;
    @Expose
    private Date LastUpdate;
    @Expose
    private List<FieldModel> Fields;
    @Expose
    private Long CropTypeId;
    @Expose
    private String Observations;
    @Expose
    private Boolean Deleted;
    @Expose
    private Integer Year;

    // Local id for searching after upload and set the id received
    @Expose
    private Long LocalId;
    ....

在此之后,我向我的GsonBuilder添加了excludeFieldsWithoutExposeAnnotation(),因此只有具有@Expose注释的字段将被序列化或反序列化:

    new GsonBuilder()
            .registerTypeAdapter(Date.class, new GsonDefaultTimeZoneAdapter())
            .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
            .excludeFieldsWithoutExposeAnnotation()
            .create();