java 对象转换 字段相同,使用jackson将java对象转换为json字符串时,重复的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) ;

}

}

Now, i get result

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());

}

But the result's duplicated properties:

{"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 ?

解决方案

I add

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

before

public class LogLikeArticleDetail extends BaseObject

and the result that i want.

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) ;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值