如何维护字符串Java的格式

时间:2023-01-14 10:46:01

So I'm parsing a JSON string to a java string and printing it. I'm using the following method to do that.

所以我正在将一个JSON字符串解析为java字符串并打印它。我正在使用以下方法来做到这一点。

JSONParser parser=new JSONParser();
     Object obj = parser.parse(output);
     JSONObject jsonObject = (JSONObject) obj;
     String stdout= (String) jsonObject.get("Stdout");
     String stderr= (String) jsonObject.get("Stderr");

     out.print(stdout);
     out.print(stderr);

This is my JSON string:

这是我的JSON字符串:

{"Stdout":"/mycode.c: In function 'main':\n/mycode.c:8:5: error: expected ';' before 'return'\n     return 0;\r\n     ^\nsh: 1: ./myapp: not found\n","Stderr":"exit status 127"}

When I use System.out.print(stdout) and System.out.print(stdout) I get my desired format of output in the console. That is: 如何维护字符串Java的格式

当我使用System.out.print(stdout)和System.out.print(stdout)时,我在控制台中获得了所需的输出格式。那是:

But now obviously I want it on my webpage so I do out.print(stdout) instead. But I don't get the desired format. Instead it just shows a single line. See picture: 如何维护字符串Java的格式

但现在很明显我想在我的网页上找到它,所以我做了out.print(stdout)。但我没有得到理想的格式。相反,它只显示一行。见图:

Any ideas how to fix this?

任何想法如何解决这一问题?

1 个解决方案

#1


1  

Your webpage is HTML, so your /r/n aren't being treated as line breaks.

您的网页是HTML,因此您的/ r / n不会被视为换行符。

You could replace all of the \r\n with <br> tags to force new lines.

您可以使用
标记替换所有\ r \ n以强制换行。

Or put your whole message in a <PRE> tag, which will render it as plain boring text and not HTML content. This is probably the safer option, because the content could contain other characters or text that might upset HTML parsing by the browser:

或者将整个消息放在

标记中,这将使其成为简单无聊的文本,而不是HTML内容。这可能是更安全的选项,因为内容可能包含可能扰乱浏览器HTML解析的其他字符或文本:

out.print("<PRE>" + stdout + "</PRE>");

#1


1  

Your webpage is HTML, so your /r/n aren't being treated as line breaks.

您的网页是HTML,因此您的/ r / n不会被视为换行符。

You could replace all of the \r\n with <br> tags to force new lines.

您可以使用
标记替换所有\ r \ n以强制换行。

Or put your whole message in a <PRE> tag, which will render it as plain boring text and not HTML content. This is probably the safer option, because the content could contain other characters or text that might upset HTML parsing by the browser:

或者将整个消息放在

标记中,这将使其成为简单无聊的文本,而不是HTML内容。这可能是更安全的选项,因为内容可能包含可能扰乱浏览器HTML解析的其他字符或文本:

out.print("<PRE>" + stdout + "</PRE>");