【问题标题】:Trying to set Json Ignore for some of the field in class试图为课堂上的某些领域设置 Json Ignore
【发布时间】:2015-08-12 14:14:38
【问题描述】:

我在这里尝试将类转换为 json 字符串。在转换期间,对于一个字段,我希望设置 @JsonIgnore

Campaign 类包括:

int campaignId;
Date startDate;
Date endDate;

我现在就是这样做的

String data = JacksonObjectMapper.INSTANCE.getObjectMapper().writeValueAsString(Campaign);

我只想通过对象而不是通过注释来使用它,因为我有条件地这样做

【问题讨论】:

    标签: java json annotations jackson jsonobject


    【解决方案1】:

    我已经使用了 Gson。它有一些内部函数来处理元素和对象。

    Gson gson = new GsonBuilder().create();
    JsonElement jsonElement = gson.toJsonTree(object);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    jsonObject.remove("endDate");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2019-05-06
      • 2021-03-20
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多