疑问
- 有时候需要一个实体类的json格式,如给前端写json形式的请求实例需要完整的json字符串
- 但是在用Json工具类转换的时候,value为null的时候是不显示的
- 如:
String json = JSONObject.toJSONString(object);
- 返回:
{"isAsc":"asc","orderBy":"","params":{}}
个人解决方案
- 注意这段代码(
JSON_STYLE
):return new ToStringBuilder(this,ToStringStyle.JSON_STYLE)
public String toJson() {
return new ToStringBuilder(this,ToStringStyle.JSON_STYLE)
.append("id", getId())
.append("title", getTitle())
.append("surfacePlot", getSurfacePlot())
.append("content", getContent())
.append("moduleId", getModuleId())
.append("moduleName", getModuleName())
.append("channelName", getChannelName())
.append("categoryId", getCategoryId())
.append("categoryName", getCategoryName())
.append("releaseStatus", getReleaseStatus())
.append("updateStatusTime", getUpdateStatusTime())
.append("recommended", getRecommended())
.append("recommendedTime", getRecommendedTime())
.append("readingQuantity", getReadingQuantity())
.append("source", getSource())
.append("createTime", getCreateTime())
.append("opUser", getOpUser())
.toString();
}
{
"id": null,
"title": null,
"surfacePlot": null,
"content": null,
"moduleId": null,
"moduleName": null,
"channelName": null,
"categoryId": null,
"categoryName": null,
"releaseStatus": null,
"updateStatusTime": null,
"recommended": null,
"recommendedTime": null,
"readingQuantity": null,
"source": null,
"createTime": null,
"opUser": null
}
public String toJson() {
return new ToStringBuilder(this,ToStringStyle.JSON_STYLE)
.append("id", "1")
.append("title", "1")
.append("surfacePlot", "1")
.append("content", "1")
.append("moduleId", "1")
.append("moduleName", "1")
.append("channelName", "1")
.append("categoryId", "1")
.append("categoryName", "1")
.append("releaseStatus", "1")
.append("updateStatusTime", "1")
.append("recommended", "1")
.append("recommendedTime", "1")
.append("readingQuantity", "1")
.append("source", "1")
.append("createTime", "1")
.append("opUser", "1")
.toString();
}