ackson将java对象转换为json字符串时重复json属性

ackson将java对象转换为json字符串时重复json属性

本文介绍了使用jackson将java对象转换为json字符串时重复json属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Pojo 对象,使用 getAsJson 函数返回此对象的 Json 字符串.我使用 JsonProperty 在这个对象中定义 json 属性.使用 ObjectMapper 的 writeValueAsString 为该对象写入 json 字符串.

I have Pojo object, with getAsJson function to return Json string for this object.I use JsonProperty to define json properties in this object.Use writeValueAsString of ObjectMapper to write json string for this object.

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown=true)
public class LogLikeArticleDetail extends BaseObject {
    private static final long serialVersionUID = -2018373118257019033L;
    @JsonProperty("LikeArticleGUId")
    private String likeArticleGUId;
    @JsonProperty("UserId")
    private String userID;
    @JsonProperty("UserName")
    private String userName;
    @JsonProperty("IP")
    private String ip;
    @JsonProperty("OS")
    private String os;
    @JsonProperty("UserAgent")
    private String userAgent;
    @JsonProperty("WebsiteCode")
    private String websiteCode;
    @JsonProperty("ArticleId")
    private String articleID;
    @JsonProperty("ATitle")
    private String aTitle;
    @JsonProperty("CateAlias")
    private String cateAlias;
    @JsonProperty("LikeStatus")
    private String likeStatus;
    @JsonProperty("TimeStamp")
    private Date timeStamp;
        //get, set....
        //....
        @JsonIgnore
    public String getAsJSON() throws JsonGenerationException, JsonMappingException,    IOException{
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(this) ;
    }
}

现在,我得到结果

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        Calendar calendar =  Calendar.getInstance();
        LogLikeArticleDetail logLikeArticle = new LogLikeArticleDetail("1","2","3","4","5","6","7","8","what thing "nothing" show","10","11",calendar.getTime());
        System.out.println(logLikeArticle.getAsJSON());
    }

但是结果的重复属性:

{"LikeArticleGUId":"1","UserId":"2","UserName":"3","IP":"4","OS":"5","UserAgent":"6","WebsiteCode":"7","ArticleId":"8","ATitle":"what thing "nothing" show","CateAlias":"10","LikeStatus":"11","TimeStamp":1352256727062,"_likeArticleGUId":"1","websiteCode":"7","likeStatus":"11","userID":"2","userName":"3","ip":"4","os":"5","userAgent":"6","articleID":"8","aTitle":"what thing "nothing" show","cateAlias":"10","timeStamp":1352256727062}

告诉我这个问题发生了什么?

Show me what's occur in this problem ?

推荐答案

所以我遵循:如何指定 jackson 仅使用字段 - 最好是全局的

我添加

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

之前

public class LogLikeArticleDetail extends BaseObject

以及我想要的结果.

另一个可以在 getAsJson() 函数中解决这个问题,例如:

So can another solve that in getAsJson() function like:

ObjectMapper mapper  = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
return mapper.writeValueAsString(this) ;

感谢@Sean Carpenter 的问题和@kmb385 在上面链接中的回答.

Thanks for @Sean Carpenter 's question and @kmb385 answer in link above.

这篇关于使用jackson将java对象转换为json字符串时重复json属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:40