javascript 使用 json_encode 输出换行符或换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25587496/
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-10-28 04:42:19  来源:igfitidea点击:

Output line breaks or new lines with json_encode?

javascriptphpjqueryjson

提问by EternalHour

I am new to JSON format so please forgive my lack of knowledge. But somehow, I need to add some line breaks to my json_encode array so that it will parse with multiple lines. I've searched and searched but nothing fits my situation.

我是 JSON 格式的新手,所以请原谅我缺乏知识。但不知何故,我需要在我的 json_encode 数组中添加一些换行符,以便它可以解析多行。我搜索了又搜索,但没有什么适合我的情况。

It's outputted as:

它输出为:

Following formats accepted: www.website.com website.com website.net/something

But I'm looking to output the data like this:

但我希望输出这样的数据:

Website needs to be in the following formats:  
www.website.com  
website.com  
website.net/something

I've tried:

我试过了:

echo json_encode( 
    array( 'status' => 'failed',
           'message' => "Website needs to be in the following formats:\n
            www.website.com\n
            website.com\n
            website.net/something"
    ), JSON_PRETTY_PRINT);

But Javascript is parsing it as a literal string so the newlines are ignored and outputted. Can I send the data from PHP to javascript using straight JSON format or do I have to use an array?

但是 Javascript 将其解析为文字字符串,因此换行符将被忽略并输出。我可以使用直接的 JSON 格式将数据从 PHP 发送到 javascript 还是必须使用数组?

I've also tried using <br />.

我也试过使用<br />.

EDIT :
I am outputting the following way:

编辑:
我输出以下方式:

$.ajax({
    url: "/forms/content_process.php",
    type:'POST',
    data: $(".content_cat").serialize(),
    processData: false,
    dataType: "json",
    success: function(response) {
        if (response.status == "failed") {
            $(".content_status").css( "background-color", "red" );
            $(".content_status").text(response.message).slideDown("normal");
        } else {
            $(".content_status").css( "background-color", "green" );
            $(".content_status").text("Your category was submitted!").slideDown("normal");
        }
    }
});

回答by dreamlab

use \nto insert newline character into the string. don't add a real newline as well.

用于\n在字符串中插入换行符。也不要添加真正的换行符。

echo json_encode( 
    array( 'status' => 'failed',
           'message' => "Website needs to be in the following\nformats:\nwww.website.com\nwebsite.com\nwebsite.net/something"
    ), JSON_PRETTY_PRINT);

than on the client side before inserting it into your dom, you will need to replace the newlines with <br />or use white-space: pre. alternatively you could surround each line with paragraph <p></p>tags. to do so have a look here: jQuery text() and newlines.

与在客户端插入 dom 之前相比,您需要将换行符替换为<br />或使用white-space: pre. 或者,您可以用段落<p></p>标签围绕每一行。为此,请查看此处:jQuery text() 和换行符

回答by EternalHour

Looking back at this question, it became painfully obvious to me how simple the solution was. The reason I had such a difficulty, was because I needed to use htmlformat.

回顾这个问题,我很清楚这个解决方案是多么简单。我有这样困难的原因是因为我需要使用html格式。

Of course it wasn't honoring <br />, I was formatting it as text! This was the simplest way to do it for my situation.

当然,这不是尊重<br />,我将其格式化为文本!对于我的情况,这是最简单的方法。

array( 'status' => 'failed',
       'message' => "Website needs to be in the following formats:<br />
        www.website.com<br />
        website.com<br />
        website.net/something"
), JSON_PRETTY_PRINT);

$(".content_status").html(response.message).slideDown("normal");