我在印刷方面遇到了一些麻烦。在印刷方面,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}
]
}发布于 2019-01-23 21:09:41
好吧,目前它只是在进行中,但是对于只使用一个数组的字符串来说,这应该是有用的。将研究如何使它更稳定和能够处理更复杂的结构。
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}
]
}https://stackoverflow.com/questions/54327375
复制相似问题