繁体   English   中英

如何在JSP页面中显示Pretty Print JSON String

[英]how to show Pretty Print JSON String in a JSP page

我想在JSP页面中显示JSON字符串。 我正在使用Spring MVC框架。 下面是代码

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));

此行System.out.println(indented); 打印出这样的东西-

{
  "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"
  } ]
}

这就是我需要使用JSP页面显示的方式。 但是当我将它添加到这样的模型中时

model.addAttribute("response", (indented));

然后将其显示在resultform.jsp页面中,如下所示-

    <fieldset>
        <legend>Response:</legend>
            <strong>${response}</strong><br />

    </fieldset>

我在浏览器上得到了类似的东西-

{ "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" } ] }

我不需要的 我不知道为什么会这样包裹它? 我需要这样打印出来的方式。

{
  "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"
  } ]
}

谁能告诉我为什么会这样发生? 以及如何在JSP页面中以所需方式显示它?

空格字符通常在HTML中折叠(默认情况下)。 这样的字符包括换行符。 将JSON转储到<pre>

<pre>
${response}
</pre>

其他解决方案: https : //stackoverflow.com/a/10474709/139010

暂无
暂无

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

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