Phate
Phate

Reputation: 6622

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?

Upvotes: 9

Views: 10045

Answers (2)

Brad
Brad

Reputation: 163488

Untested, but should get you started:

$('#myDiv').append(
    $('<pre>').text(
        JSON.stringify(parsedJson, null, '  ')
    )
);

Upvotes: 16

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

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

Upvotes: 5

Related Questions