Java GSON的自定义打印格式
我和格森在印刷方面有点问题。GSON在打印方面有两种选择Java GSON的自定义打印格式,java,gson,Java,Gson,我和格森在印刷方面有点问题。GSON在打印方面有两种选择 漂亮的印刷品 紧凑印刷 我打算使用一种经过修改的Pretty打印形式,即使sayJsonPrintFormatter是用于修改输出格式的类。我在GSON存储库中找不到该类 你知道为什么会这样吗?或者我可以修改GSON打印? 除此之外,任何用于修改Java语言中JSON的间距或格式的库都会很有帮助 漂亮的印花: { "classname": "something", "typ
{
"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}
]
}
好的,目前这只是在进行中,但是对于只有一个数组的字符串,这应该可以做到。将研究如何使其更稳定,并能够处理更复杂的结构
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);
}
结果应该是这样的(至少对于我的简单类):
好的,目前这只是在进行中,但是对于只有一个数组的字符串,这应该可以做到。将研究如何使其更稳定,并能够处理更复杂的结构
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);
}
结果应该是这样的(至少对于我的简单类):
这部分不说明你不能吗<代码>如果要使用“漂亮打印”功能,必须使用GsonBuilder配置Gson实例。JsonFormatter没有通过我们的公共API公开,因此客户端无法配置JSON输出的默认打印设置/边距。我记得读过这篇文章,但如果有办法使用第三方库或其他东西!我相信总有一种方法(虽然我不知道lib),但您的用例是什么,需要json的这种特定样式?长话短说:POJO到json。许多字段名(属性)希望最终用户能够更容易地阅读,如上面的“我的打印样式”所示。Imo pretty Print非常可读,我还将其转换为YAML(高度可读)但是,在我做出放弃此任务的最终决定之前,我想询问一下Stackoverflow,因为这是一个非常小的更改,但付出了巨大的努力。今晚,我将添加一个函数使之成为可能,有趣的练习这部分不是澄清了你不能吗<代码>如果要使用“漂亮打印”功能,必须使用GsonBuilder配置Gson实例。JsonFormatter没有通过我们的公共API公开,因此客户端无法配置JSON输出的默认打印设置/边距。我记得读过这篇文章,但如果有办法使用第三方库或其他东西!我相信总有一种方法(虽然我不知道lib),但您的用例是什么,需要json的这种特定样式?长话短说:POJO到json。许多字段名(属性)希望最终用户能够更容易地阅读,如上面的“我的打印样式”所示。Imo pretty Print非常可读,我还将其转换为YAML(高度可读)但是在我做出放弃此任务的最终决定之前,我想询问一下Stackoverflow,因为这是一个非常小的更改,但付出了巨大的努力。今晚我将添加一个函数使其成为可能,有趣的练习我可以接受这一回答,因为我可以在运行时获得数组的大小(即拆分)!谢谢你,斯蒂芬!:)荣誉@Abubakhan:小心这个解决方案的陷阱!在许多情况下,它会中断,例如,如果其中一个值包含
“[”
。我可以接受这一回答,因为我可以在运行时获得数组的大小(即拆分)!谢谢你Stefan!:)太好了!@AbuBakarKhan:小心这个解决方案的缺陷!在很多情况下,它会崩溃,例如,如果其中一个值包含“[”
。