Java Json Format formatJson(String content)

Here you can find the source of formatJson(String content)

Description

format Json

License

Apache License

Declaration

private static String formatJson(String content) throws IOException 

Method Source Code

//package com.java2s;
/*// w  w w .j  a  v a 2 s. c  o m
 * LearningStudio HelloWorld Application & API Explorer 
 * 
 * Need Help or Have Questions? 
 * Please use the PDN Developer Community at https://community.pdn.pearson.com
 *
 * @category   LearningStudio HelloWorld
 * @author     Wes Williams <wes.williams@pearson.com>
 * @author     Pearson Developer Services Team <apisupport@pearson.com>
 * @copyright  2014 Pearson Education Inc.
 * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache 2.0
 * @version    1.0
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Main {
    private static String formatJson(String content) throws IOException {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(content).getAsJsonObject();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        return json == null ? "" : gson.toJson(json);
    }
}

Related

  1. formatJson(Object object)
  2. formatJSON(String jsonData)