欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中如何把實(shí)體類轉(zhuǎn)成json格式的字符串

 更新時(shí)間:2023年12月29日 09:11:06   作者:陳賝  
這篇文章主要介紹了java中如何把實(shí)體類轉(zhuǎn)成json格式的字符串問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

疑問(wèn)

有時(shí)候需要一個(gè)實(shí)體類的json格式,如給前端寫(xiě)json形式的請(qǐng)求實(shí)例需要完整的json字符串

但是在用Json工具類轉(zhuǎn)換的時(shí)候,value為null的時(shí)候是不顯示的

如:

String json = JSONObject.toJSONString(object);

返回:

{"isAsc":"asc","orderBy":"","params":{}}

個(gè)人解決方案

注意這段代碼(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
}

不想有null可以自己設(shè)定統(tǒng)一的

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

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論