Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a pretty json on an html div..possible?

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?

like image 764
Phate Avatar asked Jun 16 '14 13:06

Phate


People also ask

How do I pretty-print a JSON object?

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.

What is pretty JSON format?

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 .


2 Answers

Untested, but should get you started:

$('#myDiv').append(
    $('<pre>').text(
        JSON.stringify(parsedJson, null, '  ')
    )
);
like image 125
Brad Avatar answered Oct 08 '22 05:10

Brad


You could use <pre> tag instead of div, assuming parsedJson contains all newlines and indentation.

like image 35
IProblemFactory Avatar answered Oct 08 '22 03:10

IProblemFactory