[英]how to show Pretty Print JSON String in a JSP page
I want to show a JSON string in a JSP page. 我想在JSP页面中显示JSON字符串。 I am working with Spring MVC framework.
我正在使用Spring MVC框架。 Below is the code
下面是代码
String result = restTemplate.getForObject(url.toString(), String.class);
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(result, Object.class);
String indented = mapper.defaultPrettyPrintingWriter().writeValueAsString(json);
System.out.println(indented);//This print statement show correct way I need
model.addAttribute("response", (indented));
This line System.out.println(indented);
此行
System.out.println(indented);
prints out something like this- 打印出这样的东西-
{
"attributes" : [ {
"nm" : "ACCOUNT",
"error" : "null SYS00019CancellationException in CoreImpl fetchAttributes\n java.util.concurrent.CancellationException\n\tat java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat java.util.concurrent.FutureTask.",
"status" : "ERROR"
} ]
}
which is the way I needed to be shown using a JSP page. 这就是我需要使用JSP页面显示的方式。 But when I add it to model like this-
但是当我将它添加到这样的模型中时
model.addAttribute("response", (indented));
And then shows it out in a resultform.jsp page like below- 然后将其显示在resultform.jsp页面中,如下所示-
<fieldset>
<legend>Response:</legend>
<strong>${response}</strong><br />
</fieldset>
I get something like this on the browser- 我在浏览器上得到了类似的东西-
{ "attributes" : [ { "nm" : "ACCOUNT", "error" : "null
SYS00019CancellationException in CoreImpl fetchAttributes\n
java.util.concurrent.CancellationException\n\tat
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat
java.util.concurrent.FutureTask.", "status" : "ERROR" } ] }
which I don't need. 我不需要的 I don't know why it get wrapped like this?
我不知道为什么会这样包裹它? I needed the way it got printed out above means like this.
我需要这样打印出来的方式。
{
"attributes" : [ {
"nm" : "ACCOUNT",
"error" : "null SYS00019CancellationException in CoreImpl fetchAttributes\n java.util.concurrent.CancellationException\n\tat java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat java.util.concurrent.FutureTask.",
"status" : "ERROR"
} ]
}
Can anyone tell me why it got happened like this? 谁能告诉我为什么会这样发生? And how can I show it in a JSP page the way I needed?
以及如何在JSP页面中以所需方式显示它?
White space characters are usually collapsed in HTML (by default). 空格字符通常在HTML中折叠(默认情况下)。 Such characters include newlines.
这样的字符包括换行符。 Dump the JSON into a
<pre>
. 将JSON转储到
<pre>
。
<pre>
${response}
</pre>
Other solutions: https://stackoverflow.com/a/10474709/139010 其他解决方案: https : //stackoverflow.com/a/10474709/139010
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.