简体   繁体   English

多行JSON字符串到多行Javascript字符串

[英]Multiline JSON string to multiline Javascript string

im sure this is a simple thing to do yet im still having some troubles, so im making an HTTP request to a music API, i would like to get the lyrics of a searched song, and so i did, and its working just fine, the problem is that in the console, the JSON string is in order, you know like normal lyrics. 我确定这是一件简单的事情,但仍然遇到一些麻烦,因此我向音乐API发出了HTTP请求,我想获取搜索到的歌曲的歌词,所以我做到了,并且它的工作正常,问题是在控制台中,JSON字符串是正确的,就像普通歌词一样。

But when i take that perfectly ordered string and put it into a Javascript variable and print it on screen, the lyrics appears in single lines like so: 但是,当我采用完美排序的字符串并将其放入Javascript变量并将其打印在屏幕上时,歌词会像这样单行显示:

This is the verse 1 This is the verse 2 This is the verse 3 This is the verse 4 This is the verse 5 and so on. 这是诗句1这是诗句2这是诗句3这是诗句4这是诗句5依此类推。

So this is my code. 这就是我的代码。

axios.get(urlLyric, { 'headers': headers })
    .then(function (response) {
        let lyrics = response.data.lyrics_body;
        console.log(lyrics);
        $(".lyrics").html(`<p>${lyrics}</p>`)
    })
    .catch(function (err) {
        console.log(err);
    });

控制台日志 JSON回应

if lyrics is an array, then you can do this 如果lyrics是数组,那么您可以执行此操作

EDIT : add an alternative, if lyrics is just text with new line 编辑:如果lyrics只是带有换行符的文本,请添加替代项

EDIT #2 : escape HTML entities from lyrics body 编辑#2:lyrics主体转义HTML实体

    axios.get(urlLyric, { 'headers': headers })
    .then(function (response) {
        let lyrics = response.data.lyrics_body;
        console.log(lyrics);
        // loop lyrics (if lyrics is an array)
        /*
        for(var x=0; x<lyrics.length; x++) {
            $(".lyrics").append('<p>'+lyrics[x]+'</p>');
        }
        */
        // if lyrics is just text with new line, escape HTML entities & replace newline character with br.
        $(".lyrics").html('<p>'+lyrics.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/gi,'<br>')+'</p>');
    })
    .catch(function (err) {
        console.log(err);
    });

The problem has absolutely nothing to do with JSON, nor the fact that you got your data from an API. 这个问题与JSON完全无关,也与您从API获取数据无关。 The problem is that you're trying to use plain text in the context of HTML. 问题是您正在尝试在HTML上下文中使用纯文本。

If I had the following HTML: 如果我有以下HTML:

<p>
  Line 1
  Line 2
</p>

You would see text on the page as: 您会在页面上看到以下文本:

Line 1 Line 2

There are a few ways to solve this problem. 有几种方法可以解决此问题。 One of which is to change the way whitespace is handled with CSS. 其中之一是更改CSS处理空白的方式。

white-space: pre-line

If you use pre-line , it will format the text into multiple lines like you're looking for. 如果使用pre-line ,它将按照您的需要将文本格式设置为多行。

An alternative way to solve this is to convert your text into actual structured markup. 解决此问题的另一种方法是将文本转换为实际的结构化标记。 Is one line of lyrics a paragraph though? 一行歌词是段落吗? I don't think so, but this is undoubtedly debatable. 我不这么认为,但这无疑是值得商bat的。

No matter which route you take, never simply concatenate text into the context of HTML. 无论采用哪种方法,都不要简单地将文本连接到HTML上下文中。 It must be escaped first. 必须先将其转义。 Take your code here for example: 以您的代码为例:

        $(".lyrics").html(`<p>${lyrics}</p>`)

What if the song lyrics contained something like <script src="something-evil.js"></script> ? 如果歌曲歌词中包含<script src="something-evil.js"></script>怎么办? You obviously wouldn't want the ability for an external API to hijack your entire site. 您显然不希望外部API能够劫持整个站点。 You have to escape that text. 您必须转义该文本。

The easiest way to do this in jQuery is to call .text() instead. 在jQuery中最简单的方法是调用.text() Something like this: 像这样:

$('.lyrics').append(
  $('<p>').text(lyrics)
);

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

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