jsondart

How do you get a formatted/indented JSON String from a JSON object?


Using dart:convert I can get an unindented string with this code.

var unformattedString = JSON.encode(jsonObject);

How do I take a JSON object and convert it to an indented string?


Solution

  • One way to do it is by creating a JSONEncoder.withIndent instance.

    String getPrettyJSONString(jsonObject){
       var encoder = new JsonEncoder.withIndent("     ");
       return encoder.convert(jsonObject);
    }