• 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"
      ]
    }
     
     
  • 相关阅读:
    TOYS POJ
    口罩发放
    Qin Shi Huang's National Road System HDU
    次小生成树
    ACM Contest and Blackout UVA
    Strongly connected HDU
    tarjan
    Network HDU
    【洛谷 1351】联合权值
    【洛谷 3884】二叉树问题
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/5365414.html
Copyright © 2020-2023  润新知