zoukankan      html  css  js  c++  java
  • json格式化

    jar包:gson-xxx.jar
     
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParser;
    /**
     * @author henrypoter
     *
     */
    public class JsonFormatter {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
            String uglyJSONString = "{"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}";
            String prettyJsonString = jsonFormatter(uglyJSONString);
            System.out.println("JSON格式化前:");
            System.out.println(uglyJSONString);
            System.out.println("JSON格式化后:");
            System.out.println(prettyJsonString);
     
        }
         
        public static String jsonFormatter(String uglyJSONString){
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JsonParser jp = new JsonParser();
            JsonElement je = jp.parse(uglyJSONString);
            String prettyJsonString = gson.toJson(je);
            return prettyJsonString;
        }
     
    }
     
     
    结果:
    JSON格式化前:
    {"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}
    JSON格式化后:
    {
      "data1": 100,
      "data2": "hello",
      "list": [
        "String 1",
        "String 2",
        "String 3"
      ]
    }
     
     
  • 相关阅读:
    OilPaint(转载/实验)
    UE4 3D artist
    render pipeline about (翻译)
    Python 相对导入 碎碎念
    USF, USH Grammar
    JZ19 顺时针打印矩阵
    JZ49 把字符串转换成整数
    JZ45 扑克牌顺子
    JZ53 表示数值的字符串
    JZ48 不用加减乘除做加法
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/5365414.html
Copyright © 2011-2022 走看看