使用jackson将java对象转换为json字符串时重复的json属性

时间:2012-11-07 03:18:18

标签: java json properties duplicates jackson

我有Pojo对象,使用getAsJson函数返回此对象的Json字符串。 我使用JsonProperty来定义此对象中的json属性。 使用ObjectMapper的writeValueAsString为该对象编写json字符串。

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}

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

4 个答案:

答案 0 :(得分:16)

所以我遵循: how to specify jackson to only use fields - preferably globally

我添加

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

public class LogLikeArticleDetail extends BaseObject

和我想要的结果。

另一个解决方案可以在getAsJson()函数中解决:

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答案。

答案 1 :(得分:2)

您也可以使用注释为每个POJO执行此操作。将此字符串添加到您不希望自动检测的类的顶部:

  

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

例如:

@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE,
        setterVisibility=JsonAutoDetect.Visibility.NONE, creatorVisibility=JsonAutoDetect.Visibility.NONE)
class Play {
    @JsonProperty("Name")
    private String name; 

    @JsonProperty("NickName")
    private String nickName; 

    public Play(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

这将返回我定义的属性,而不是自动检测字段名称并将它们添加到我返回的JSON结果中。

答案 2 :(得分:1)

我们也可以直接在吸气剂上使用@JsonProperty(“ Name”)注释,以避免重复。

答案 3 :(得分:1)

这实际上不是问题。因此,在这里发生的事情是Jackson库无法自动匹配这些字段(没有大小写统一的假设),因此最终得到的属性是预期的两倍。

此问题的简单解决方法是仅向getters / setter添加注释(都可以)。

@JsonProperty("UserName")
public String getUserName() {
        return this.userName;
}

Jackson Github回购中也提出了这个问题。您可以在以下链接中找到答案。

https://github.com/FasterXML/jackson-databind/issues/1609