本文最后更新于:1 个月前
value值带双引号
ALT+INS 打开生成弹窗选择toString(),点击设置->点击模板->点击添加模板,设置名称将下面模板添加到代码体中即可,该方法会除数组外字段给key对应value添加双引号""
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public java.lang.String toString() { return "{" #if ( $class.hasSuper ) "\"super\":"+super.toString()+", " #end #set ($i = 0) #foreach ($member in $members) #if ($i == 0) +"\"$member.name\":"## #else +", \"$member.name\":"## #end #if ($member.primitiveArray || $member.objectArray) +java.util.Arrays.toString($member.name) #else +"\""+$member.accessor+"\"" #end #set ($i = $i + 1) #end +'}'; }
|
生成代码格式如下:
1 2
| {"font":"{"color":"0xFFFFFFFF", "size":"12", "name":"微软雅黑", "style":"1"}", "background":"true", "backgroundColor":"0xFF000000", "border":"true", "borderColor":"null", "xAlignment":"null", "yAlignment":"null", "dimension":"null", "id":"下拉列表1"}
|
注意: 已知当字段为如下类型生成的字符串存在无法json格式化,删除即可
- org.json.JSONArray
value值不带双引号
除String字段其余字段直接拼接,执行字段类型默认toString方法,设置方法同上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public java.lang.String toString() { return "{" #if ( $class.hasSuper ) "\"super\":"+super.toString()+", " #end #set ($i = 0) #foreach ($member in $members) #if ($i == 0) +"\"$member.name\":"## #else +", \"$member.name\":"## #end #if ($member.primitiveArray || $member.objectArray) +java.util.Arrays.toString($member.name) #elseif ( $member.string ) +"\""+$member.accessor+"\"" #else +$member.accessor #end #set ($i = $i + 1) #end +'}'; }
|