【问题标题】:@JsonInclude(JsonInclude.Include.NON_NULL does not work for double@JsonInclude(JsonInclude.Include.NON_NULL 不适用于双精度
【发布时间】:2015-05-24 08:16:10
【问题描述】:

我正在尝试在route!=0 的情况下检索{"status":201},但我得到{"status":201,"distance":0.0} 我如何使用@JsonInclude 或Jackson 也没有"distance":0.0 来达到它?

SDBean bean = new SDBean();
if (routeD != 0) {
    // Insert the data into the bus table
    bean.status = db.insertData(macD, routeD, latD, longD);

    return Response.status(bean.status).entity(bean.toJson()).build();

}

SDBean 类:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class SDBean {

    public int status;
    public ArrayList<Integer> routes;
    public double distance;

    public SDBean(){
    status = 230;
}

    public String toJson() {

        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();

        }
         System.out.println(json);
        return json;
    }
}

【问题讨论】:

  • 你能展示你的 Json 对象吗?
  • @JordiCastilla:你对 json 对象有什么看法?杰克逊图书馆创建了所有内容后,我没有?

标签: java json jersey jackson


【解决方案1】:

使用Double。基元 (double) 不能为空(它们有默认值)

【讨论】:

  • 或者OptionalDouble 以明确意图? (虽然你也需要注册 Jdk8Module)
猜你喜欢
  • 2021-01-14
  • 2014-01-20
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2014-01-02
  • 2019-06-03
  • 1970-01-01
相关资源
最近更新 更多