简体   繁体   English

HTML:加载 JSON<pre> 和 element; adding line breaks element; adding line breaks

[英]HTML: Loading JSON in <pre> and <code> element; adding line breaks

I want to display the content of a json file on a web page nicely.我想在网页上很好地显示 json 文件的内容。 That's how I do it:我就是这样做的:

{
  "name": "Hans",
  "age": 12,
  "favorite food": "Spätzlepfanne",
  "siblings": false
}

(silly example ... I know :D) (愚蠢的例子......我知道:D)

function loadJSON(url, callback) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType('application/json');
    xobj.open('GET', url, true);
    xobj.onreadystatechange = function() {
        if (xobj.readyState == 4 && xobj.status == '200') {
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}

and the HTML-Stuff:和 HTML 的东西:

<pre><code id="code1"></code></pre>
<script>
    loadJSON("../v0.1/json/hans.json", function(resText){
        data_parsed = JSON.parse(resText);
        //data.age = 33;
        data_stringified = JSON.stringify(data_parsed);
        document.getElementById("code1").innerHTML = resText;
    })
</script>

You probably recognized another issue: I am displaying the resText , which I am not able to change once it's loaded.您可能意识到了另一个问题:我正在显示resText ,一旦加载就无法更改。 I would like to display either the JavaScript object ( data_parsed ) to let Hans age a bit, like in the comment, or the stringified object.我想显示 JavaScript 对象 ( data_parsed ) 以让 Hans 变老一点,就像在评论中一样,或者显示字符串化对象。

The main problem is, that the output on my web page is:主要问题是,我网页上的输出是:

[object Object] for ...innerHTML = data_parsed [object Object] for ...innerHTML = data_parsed

or或者

{"name":"Hans","age":12,"favorite food":"Spätzlepfanne","sibli (a long one liner) for ...innerHTML = data_stringified {"name":"Hans","age":12,"favorite food":"Spätzlepfanne","sibli(长的一行)用于 ...innerHTML = data_stringified

I really want to be the information about Hans displayed like in that example: https://jsoneditoronline.org/#/我真的很想成为那个例子中显示的关于汉斯的信息: https : //jsoneditoronline.org/#/

A nicely and cool looking field for code, where the JSON is being displayed and with the opportunity to change something.一个漂亮而酷的代码字段,其中显示 JSON 并有机会更改某些内容。

My idea: something like "add line break after every ','" ... I don't know我的想法:类似于“在每个','之后添加换行符”......我不知道

I am open for better approaches.我愿意接受更好的方法。 :) Thanks in advance! :) 提前致谢!

First, when you are stringifying the data, you should use data_parsed rather than data .首先,当您对数据进行字符串化时,您应该使用data_parsed而不是data Also, JSON.stringify takes the third argument as the number of spaces you want while beautifying JSON and making a string that you can set to innerHTML .此外, JSON.stringify将第三个参数作为您想要的空格数,同时美化 JSON 并制作可以设置为innerHTML的字符串。

You can check the below snippet.您可以检查以下代码段。

data_parsed = JSON.parse(resText);
//data.age = 33;
data_stringified = JSON.stringify(data_parsed, null, 4);
document.getElementById("code1").innerHTML = data_stringified;

That should work.那应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM