我想在 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 页面中显示它?