I JSON.stringify
a json object by
result = JSON.stringify(message, my_json, 2)
The 2
in the argument above is supposed to pretty print the result. It does this if i do something like alert(result)
. However, I want to output this to the user by appending it inside a div. When I do this I get just a single line showing up. (I don't think it is working because the breaks and spaces are not being interpreted as html?)
{ "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 }
Is there a way to output the result of JSON.stringify
to a div in a pretty print way?
Please use a <pre>
tag
demo : http://jsfiddle.net/K83cK/
var data = {
"data": {
"x": "1",
"y": "1",
"url": "http://url.com"
},
"event": "start",
"show": 1,
"id": 50
}
document.getElementById("json").innerHTML = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
Make sure the JSON output is in a <pre>
tag.
Full disclosure I am the author of this package but another way to output JSON or JavaScript objects in a readable way complete with being able skip parts, collapse them, etc. is nodedump
, https://github.com/ragamufin/nodedump
If this is really for a user, better than just outputting text, you can use a library like this one https://github.com/padolsey/prettyprint.js to output it as an HTML table.
Consider your REST API returns:
{"Intent":{"Command":"search","SubIntent":null}}
Then you can do the following to print it in a nice format:
<pre id="ciResponseText">Output will de displayed here.</pre>
var ciResponseText = document.getElementById('ciResponseText');
var obj = JSON.parse(http.response);
ciResponseText.innerHTML = JSON.stringify(obj, undefined, 2);
My proposal is based on:
- replace each '\n' (newline) with a <br>
- replace each space with
var x = { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 };
document.querySelector('#newquote').innerHTML = JSON.stringify(x, null, 6)
.replace(/\n( *)/g, function (match, p1) {
return '<br>' + ' '.repeat(p1.length);
});
<div id="newquote"></div>
use style white-space: pre
the <pre>
tag also modifies the text format which may be undesirable.
If your <pre>
tag is showing a single-line of JSON because that's how the string is provided already (via an api or some function/page out of your control), you can reformat it like this:
HTML:
<pre id="json">{"some":"JSON string"}</pre>
JavaScript:
(function() {
var element = document.getElementById("json");
var obj = JSON.parse(element.innerText);
element.innerHTML = JSON.stringify(obj, undefined, 2);
})();
or jQuery:
$(formatJson);
function formatJson() {
var element = $("#json");
var obj = JSON.parse(element.text());
element.html(JSON.stringify(obj, undefined, 2));
}
you can try this repository : https://github.com/amelki/json-pretty-html
print the state of a component with JSX
render() {
return (
<div>
<h1>Adopt Me!</h1>
<pre>
<code>{JSON.stringify(this.state, null, 4)}</code>
</pre>
</div>
);
}
来源:https://stackoverflow.com/questions/16862627/json-stringify-output-to-div-in-pretty-print-way