0 0

Jackson 解析时过滤属性值为0的属性字段0(解决冲20元话费)0

原文描述:
     http://www.iteye.com/problems/98737

问题补充:jackson 版本2.2.3
2013年11月11日 15:04

2个答案 按时间排序 按投票排序

0 0

mapper.setSerializationInclusion(Include.NON_DEFAULT);

2013年11月16日 11:37
0 0

可以用
    @JsonInclude(value = Include.NON_DEFAULT)

示例:

package com.ljn.base;

import java.io.IOException;
import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author lijinnan
 * @date:2013-11-11 下午5:28:27
 */
public class JacksonTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            User user = new User();
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = mapper.writeValueAsString(user);
            System.out.println("User string: " + user);
            System.out.println("JSON string: " + jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class User implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @JsonInclude(value = Include.NON_DEFAULT)
    private int a;
    @JsonInclude(value = Include.NON_DEFAULT)
    private int b;
    @JsonInclude(value = Include.NON_DEFAULT)
    private int c;
    private String s = "abc";

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    @Override
    public String toString() {
        return "User [a=" + a + ", b=" + b + ", c=" + c + ", s=" + s + "]";
    }

}


输出:
User string: User [a=0, b=0, c=0, s=abc]
JSON string: {"s":"abc"}

2013年11月11日 17:50

相关推荐

Global site tag (gtag.js) - Google Analytics