亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GSON 的自定義打印格式

GSON 的自定義打印格式

慕的地8271018 2022-05-21 16:47:31
我在打印方面遇到了 GSON 的一些問題。GSON 在打印方面有兩種選擇。漂亮的印刷緊湊型印刷我打算使用修改后的漂亮打印形式,即使文檔說JsonPrintFormatter是用于修改輸出格式的類。我在 GSON 存儲庫中找不到該類!關于為什么會這樣或者無論如何我可以修改 GSON 打印的任何想法?除此之外,任何用于修改 Java 語言中 JSON 的間距或格式的庫也會有所幫助。漂亮的印刷品:    {  "classname": "something",  "type": "object",  "version": 1,  "properties": [    {      "propertyname": "something1",      "type": "String",      "length": 255    },    {      "propertyname": "something2",      "type": "Date",      "length": 10    }  ]}緊湊型打?。簕"classname":"something","type":"object","version":1,"properties":[{"propertyname":"something1","type":"String","length":255},{"propertyname":"something2","type":"Date","length":10}]}我的印刷風格:{  "classname": "something",  "type": "object",  "version": 1,  "properties": [    {"propertyname": "something1","type": "String","length": 255},    {"propertyname": "something2","type": "Date","length": 10}  ]}
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

好吧,它現在只是在進行中,但這應該可以解決只有一個數組的字符串。將研究使其更穩定并能夠處理更復雜的結構。


  private static String reformat(String og){

    String reformattable = og;

    String[] parts = reformattable.split("\\[",2);

    String arrayPart = parts[1];

    String arrayOnly = arrayPart.split("]",2)[0];

    reformattable = arrayOnly.replaceAll("\\{\n","{");

    reformattable = reformattable.replaceAll("\",\n", "\\\",");

    reformattable = reformattable.replaceAll(" +"," ");

    reformattable = reformattable.replaceAll("\\{ ","   {");

    reformattable = reformattable.replaceAll("\n }","}");


    return og.replace(arrayOnly,reformattable);

結果應該是這樣的(至少對于我的簡單類):


{

 "classname": "test",

 "properties": [

   {"propertyname": "1", "length": 1},

   {"propertyname": "1", "length": 1}

 ]

}


查看完整回答
反對 回復 2022-05-21
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號