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?
JSON structure As described above, JSON is a string whose format very much resembles JavaScript object literal format. You can include the same basic data types inside JSON as you can in a standard JavaScript object — strings, numbers, arrays, booleans, and other object literals.
The indent parameter specifies the spaces that are used at the beginning of a line. We can use the indent parameter of json. dump() to specify the indentation value. By default, when you write JSON data into a file, Python doesn't use indentations and writes all data on a single line, which is not readable.
Use the JSON. stringify function to Display formatted JSON in HTML. If you have unformatted JSON It will output it in a formatted way. Or Use <pre> tag for showing code itself in HTML page and with JSON.
One way to do it is by creating a JSONEncoder.withIndent instance.
String getPrettyJSONString(jsonObject){
var encoder = new JsonEncoder.withIndent(" ");
return encoder.convert(jsonObject);
}
Use this. it worked for me
String prettyJson(dynamic json) {
var spaces = ' ' * 4;
var encoder = JsonEncoder.withIndent(spaces);
return encoder.convert(json);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With