SerializerFeature的个别属性
| 名称 | 含义 |
|---|---|
| QuoteFieldNames | 输出key时是否使用双引号,默认为true |
| UseSingleQuotes | 使用单引号而不是双引号,默认为false |
| WriteMapNullValue | 是否输出值为null的字段,默认为false |
| PrettyFormat | 结果是否格式化,默认为false |
| SortField | 按字段名称排序后输出。默认为false |
示例:
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
String str = "{"id": 1,"name": "张三","child": [{"id": 11,"name": "李四","child": [],"age": 40},{"id": 12,"name": "老六","child": [{"id": 21,"name": "小六","age": 14,"child":[{"id":41,"name":null,"child":null,"age":0}]}],"age": 36}],"age": 80}";
JSONObject jsonObject = JSONObject.parseObject(str);
String pretty = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println(pretty);
}
输出结果:
{
"name":"张三",
"id":1,
"age":80,
"child":[
{
"name":"李四",
"id":11,
"age":40,
"child":[]
},
{
"name":"老六",
"id":12,
"age":36,
"child":[
{
"name":"小六",
"id":21,
"age":14,
"child":[
{
"id":41,
"age":0
}
]
}
]
}
]
}



