Javascript 如何在 JSON 中使用换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45020862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:52:22  来源:igfitidea点击:

How to use a line break in JSON

javascripthtmlcssjson

提问by Jake P

I have the text on my website appear from a .json file, and I want to have the line appear as:

我的网站上的文本出现在 .json 文件中,我希望该行显示为:

Phone: 123-456-7890

电话:123-456-7890

Local: 012-35-6789

本地:012-35-6789

Instead I am getting:

相反,我得到:

Phone: 123-456-7890 Local: 012-35-6789

电话:123-456-7890 本地:012-35-6789

My code:

我的代码:

footer{
   "call_us": "Phone: {phone_number} \n Local: 012-345-6789"
}

Is there something besides \n I should be using?

除了 \n 我应该使用之外还有什么吗?

Edit:

编辑:

I am calling the JSON text to be displayed using the following code. I believe this is using Handlebars

我正在使用以下代码调用要显示的 JSON 文本。我相信这是使用把手

{{#if settings.phone_number}}
                <strong>{{lang 'footer.call_us' phone_number=settings.phone_number}}</strong>
            {{/if}}

Full Code:

完整代码:

<article class="footer-info-col footer-info-col--small" data-section-type="storeInfo">
            <h5 class="footer-info-heading">{{lang 'footer.info'}}</h5>
            <address>{{nl2br settings.address}}</address>
            <script> var footer={
                "call_us": "Phone: {phone_number} \n Local: 012-345-6789"
            }
            for (var key in footer) {
                if (footer.hasOwnProperty(key)) {
                    console.log(footer[key]);
                }
            }</script>
            <!--{{#if settings.phone_number}}
                <strong>{{lang 'footer.call_us' phone_number=settings.phone_number}}</strong>-->
            {{/if}}
        </article>

回答by mastodonbot

\r

\r

It's that simple. I've been trying to figure this out too for a mastodon bot using tracery.

就这么简单。我也一直在尝试为使用窗饰的乳齿象机器人解决这个问题。

If you use \r you'll get a carriage return.

如果你使用 \r 你会得到一个回车。

回答by ad_on_is

If you're outputting your call_uswith PHP you can use PHP's nl2br()function: http://php.net/manual/de/function.nl2br.php

如果你call_us用 PHP输出你可以使用 PHP 的nl2br()函数:http: //php.net/manual/de/function.nl2br.php

If you're outputting it with JS, you can use yourvar.replace("\n", "<br />")

如果你用JS输出它,你可以使用 yourvar.replace("\n", "<br />")

otherwise use <br />instead of \nin your JSON.

否则在您的 JSON 中使用<br />而不是\n

回答by Peter Schweitzer Jr

You should convert the \ninto an HTML break: <br>. You can do this with the String replace method.

您应该将 转换\n为 HTML 中断:<br>。您可以使用字符串替换方法执行此操作。

回答by Osama

var footer={
   "call_us": "Phone: {phone_number} \n Local: 012-345-6789"
}
for (var key in footer) {
  if (footer.hasOwnProperty(key)) {
    console.log(footer[key]);

  }
}

回答by Jake P

I solved this issue by breaking the JSON line into two separate lines, and in the HTML file adding in the second call with a <br>in between the two. I would still love to know if there is a simple command that would do this in JSON, like <br>or \nbut neither of those work for me.

我通过将 JSON 行分成两行,并在 HTML 文件中添加第二个调用并<br>在两者之间添加一个来解决这个问题。我仍然很想知道是否有一个简单的命令可以在 JSON 中执行此操作,例如<br>\n但这些都不适合我。

JSON:

JSON:

    "footer": {
    "call_us": "Phone: {phone_number}",
    "localc": "Local: 012-345-6789"
}

HTML:

HTML:

{{#if settings.phone_number}}
                <strong>{{lang 'footer.call_us' phone_number=settings.phone_number}}</strong>
                <br><strong>{{lang 'footer.localc'}}</strong>
            {{/if}}