console.log("data",data) // lets you unfold the object manually
如果您想在DOM中显示对象,您应该考虑它可能包含将被解释为超文本标记语言的字符串。
var s = JSON.stringify(data,null,2) // formatvar e = new Option(s).innerHTML // escapedocument.body.insertAdjacentHTML('beforeend','<pre>'+e+'</pre>') // display
<!-- here is a complete example pretty print with more space between lines--><!-- be sure to pass a json string not a json object --><!-- use line-height to increase or decrease spacing between json lines -->
<style type="text/css">.preJsonTxt{font-size: 18px;text-overflow: ellipsis;overflow: hidden;line-height: 200%;}.boxedIn{border: 1px solid black;margin: 20px;padding: 20px;}</style>
<div class="boxedIn"><h3>Configuration Parameters</h3><pre id="jsonCfgParams" class="preJsonTxt">\{\{ cfgParams }}</pre></div>
<script language="JavaScript">$( document ).ready(function(){$(formatJson);
<!-- this will do a pretty print on the json cfg params -->function formatJson() {var element = $("#jsonCfgParams");var obj = JSON.parse(element.text());element.html(JSON.stringify(obj, undefined, 2));}});</script>