【问题标题】:Java deserialize JSON where a variable is named "class"Java 反序列化 JSON,其中变量名为“类”
【发布时间】:2016-01-29 18:36:01
【问题描述】:

我目前正在使用 Gson 在 Java 应用程序上反序列化 JSON 数据。 问题是有一个名为class 的变量,但是Java 不允许我在我的对象(Json 将被存储到的对象)中定义一个以这种方式命名的变量。

public class InfosHistory {
    String title;
    String user;
    String content;
    String date;
    String id;
    String visible;
    String id_activite;
    String class; <--
}

【问题讨论】:

  • 您知道可以使用 GSON 手动反序列化对象吗?您不必让它自动完成。
  • 你能解释更多吗?我正在使用改造,带有 Gson 转换器

标签: java json serialization gson


【解决方案1】:

您可以使用attributes 来定义序列化名称:

public class InfosHistory {
    String title;
    String user;
    String content;
    String date;
    String id;
    String visible;
    String id_activite;

    @SerializedName("class")
    String class_name;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多