/**
* Gson对象转换为字符串并格式化<br/>
* <业务逻辑><br/>
* @param object
* @return string 返回格式化后的json字符串
* @author Jihua Yi
*/
private String getPrettyGsonStr(Object object) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(object);
}
/**
* fastJson对象转换为字符串并格式化<br/>
* <业务逻辑><br/>
* @param object
* @return string 返回格式化后的json字符串
* @author Jihua Yi
*/
private String getPrettyFastJsonStr(Object object) {
String data = JSON.toJSONString(object, true);
data = data.replace("\":\"","\": \"");
data = data.replace("\t"," ");
return data;
}
以上两种格式化后的效果:
{
“filed1”: “test1”,
“field2”: “test2”,
“field3”: “test3”
}
这也是Gson默认格式化后的效果;而FastJson默认格式化后的效果为:
{
“filed1”:“test1”,
“field2”:“test2”,
“field3”:“test3”
}