相关文章推荐
成熟的刺猬  ·  python滑动平均步长-掘金·  1 年前    · 
彷徨的热带鱼  ·  Vbs Popup ...·  1 年前    · 
//格式化字符串 fun formatJson(content: String): String { val jsonObject = JSONObject.parse(content) return JSON.toJSONString(jsonObject,true) //格式化对象 fun formatJson(content: Any): String { val json = JSON.toJSON(content) as JSONObject return JSON.toJSONString(json,true)

二、采用Gson

implementation "com.google.code.gson:gson:2.8.5"
  1. 格式化函数
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
//格式化字符串
fun formatJson(content: String): String {
    val gson = GsonBuilder().setPrettyPrinting().create()
    val jsonElement = JsonParser().parse(content)
    return gson.toJson(jsonElement)
//格式化对象
fun formatJson(content: Any): String {
    val gson = GsonBuilder().setPrettyPrinting().create()
    return gson.toJson(content)

三、采用Jackson

implementation "com.fasterxml.jackson.core:jackson-core:2.9.9"
implementation "com.fasterxml.jackson.core:jackson-databind:2.9.9"
  1. 格式化函数
import com.fasterxml.jackson.databind.ObjectMapper
//格式化字符串
fun formatJson(content: String): String {
    val mapper = ObjectMapper()
    val json = mapper.readValue(content, Any::class.java)
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json)
//格式化对象
fun formatJson(content: Any): String {
    val mapper = ObjectMapper()
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(content)

安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061
点击关注专栏,查看最新技术分享
更多技术总结好文,请关注:「程序园中猿」

SpringBoot使用Json框架(fastjsonjackson),把Json数据格式化返回到前端页面,并对Json数据中为null值的数据,进行格式为""。 * @param obj obj public static String formatJson(Object obj) throws JsonProcessingException { ObjectMapper mapper = ne java使用fastjson组装json报文的时候,如果直接.toString()打印出来字符串是很丑的,没有空格和缩进。SerializerFeature也是fastjson包里面的类。这样设置后,打印出来日志输出就会是格式化好的字符串了。 转载:https://www.sojson.com/blog/245.html Jackson 格式化输出JSON 代码说明(对象) 我们一般输出就是普通的toString 输出。如下代码: Demo demo = new Demo("sojson",4,"https://www.sojson.com"); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(demo)); 格式化类:class JsonFormat{private StringBuffer buffer=new StringBuffer();//格式jsonprivate void format(Object json,int num,boolean isArray){if(json instanceof JSONObject){JSONObject jsonObject=(JSONObject...