使用Json将Java对象转换为json字符串时重复的json属性
我有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}
告诉我这个问题发生了什么?
回答:
所以我确实遵循: 如何指定杰克逊只使用字段-最好是全局
我加
@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的答案。
以上是 使用Json将Java对象转换为json字符串时重复的json属性 的全部内容, 来源链接: utcz.com/qa/430133.html