I already read this topic How can I pretty-print JSON using JavaScript?
anyway the correct answer just works if I want to print that json to the console.
Doing something like
$("#myDiv").text(parsedJson);
or
$("#myDiv").html(parsedJson);
will not result in a pretty json.
My intention is to display a dialog box containing that json object, in an human readable form..is it possible?
We can pretty-print a JSON using the toString(int indentFactor) method of org. json. JSONObject class, where indentFactor is the number of spaces to add to each level of indentation.
Pretty printing is a form of stylistic formatting including indentation and colouring. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and for machines to parse and generate. The official Internet media type for JSON is application/json .
Untested, but should get you started:
$('#myDiv').append(
$('<pre>').text(
JSON.stringify(parsedJson, null, ' ')
)
);
You could use <pre>
tag instead of div
, assuming parsedJson
contains all newlines and indentation.
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