java对象转换字段相同_使⽤Json将Java对象转换为json字符
串时重复的json属性
我有Pojo对象,带有getAsJson函数以返回此对象的Json字符串。我使⽤JsonProperty在此对象中定义json属性。使⽤ObjectMapper 的writeValueAsString可以为此对象编写json字符串。
dehaus.jackson.JsonGenerationException;
json转换对象dehaus.jackson.annotate.JsonIgnore;
dehaus.jackson.annotate.JsonIgnoreProperties;
dehaus.jackson.annotate.JsonProperty;
dehaus.jackson.map.JsonMappingException;
dehaus.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,
//....
@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 = Instance();
LogLikeArticleDetail logLikeArticle = new LogLikeArticleDetail("1","2","3","4","5","6","7","8","what thing \"nothing\" show","10","11",Time());
System.out.AsJSON());
}
但是结果是重复的属性:
{"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","us thing \"nothing\" show","cateAlias":"10","timeStamp":1352256727062}
告诉我这个问题发⽣了什么?
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论